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

Donal O'Connor donaloconnor at gmail.com
Tue Aug 4 15:18:39 CEST 2009


You must call painter.begin() and painter.end()

Try this:

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

}

On Tue, Aug 4, 2009 at 2:06 PM, varun singh <mailthatmale at yahoo.co.in>wrote:

>
> 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/
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090804/1a19607d/attachment.html 


More information about the Qt-interest-old mailing list