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

varun singh mailthatmale at yahoo.co.in
Tue Aug 4 16:51:14 CEST 2009


hi, 

Well i tried mTank->show(), but it didnt work, and also as Ender told painter.begin() is not going to work.
But anyways, thanx Donal and _h_, i appreciate your concern, thanx to Ender too.

Well, while wandering around my code, i tried MainWindow::setCentralWidget(mTank), and it was visible. The problem is not solved but I think that gives some clue. I'm working on it. But if i can get some imputs from the members, that would be great help.

-Varun
------------------
>
> 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


      Yahoo! recommends that you upgrade to the new and safer Internet Explorer 8. http://downloads.yahoo.com/in/internetexplorer/




More information about the Qt-interest-old mailing list