[Qt-interest] Customized QWidget item not visible on QMainWindow

varun singh mailthatmale at yahoo.co.in
Tue Aug 4 15:06:40 CEST 2009


Hi, 
  Well i'm finding this one a bit difficult though it sounds simple. I'm making a game named Tanks (in which the tanks will fight).

So, i subclassed QWidget and made a function to create a tank like figure  (using rectangles) with the help of QWidget::paintEvent(). Then i initialized a Tank variable in QMainWindow class.

Now, the MainWindow runs, but shows blank.
Here's a snapshot of the code.
******************************************************************************

Tanks::Tanks(QPoint point, QWidget *parent)
        :QWidget(parent)
{
    mPosition = point;
    this->setGeometry(mPosition.x() - 25, mPosition.y() - 25, 50, 50);
    this->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

    /* mBody, mBarrel etc. are members of type QRect*/
    mBody = QRect(mPosition.x() - 15, mPosition.y() - 15, 30, 30);
    mBarrel = QRect(mPosition.x() , mPosition.y(), 30, 4);
    mWheelTop = QRect(mPosition.x() - 20, mPosition.y() - 20, 40, 10);
    mWheelBottom = QRect(mPosition.x() - 20, mPosition.y() + 10, 40, 10);
    update();
}

void Tanks::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    paintTank(painter);

}

void Tanks::paintTank(QPainter &painter)
{
    paintBody(painter);
    paintBarrel(painter);
    paintWheels(painter);
}


void Tanks::paintBody(QPainter &painter)
{
    painter.drawRect(mBody);

}

void Tanks::paintBarrel(QPainter &painter)
{
    painter.drawRect(mBarrel);
}

void Tanks::paintWheels(QPainter &painter)
{
    painter.drawRect(mWheelTop);
    painter.drawRect(mWheelBottom);
}

-----------------
/*Snippet of QMainWindow*/
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(100, 100, 1250, 650);
    mTank = new Tanks(QPoint(200, 200), this);
}

*******************************************************************************
Can anyone help?

-Varun


      Looking for local information? Find it on Yahoo! Local http://in.local.yahoo.com/




More information about the Qt-interest-old mailing list