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

Andreas Diehl Andreas_Diehl at abs.org
Tue Aug 4 17:16:31 CEST 2009


Hello Varun.
Of course you've found the solution already: Create a new widget and set it as the central widget of your QMainWindow and attach your tanks to it:

MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
{
  this->setGeometry(100, 100, 1250, 650);
  QWidget* battlefield = new QWidget(this);
  this->setCentralWidget(battlefield);

  mTank = new Tanks(QPoint(200, 200), battlefield);
}

Regards,
	Andreas


-----Ursprüngliche Nachricht-----
Von: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] Im Auftrag von varun singh
Gesendet: Dienstag, 4. August 2009 16:51
An: qt-interest at trolltech.com
Betreff: Re: [Qt-interest] Customized QWidget item not visible on QMainWindow


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/

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
--
Mit freundlichen Grüßen
Andreas Diehl 
 
-------------------------- ABS -------------------------
ABS Systemberatung OHG          Tel: +49 (0)7053/9290-03
Hauffstr. 9                     Fax: +49 (0)7053/9290-
D-75385 Bad Teinach-Zavelstein        http://www.abs.org
Registergericht Calw HRA 1031
[mailto:Andreas_Diehl at abs.org]
 
----- Loesungen fuer den Maschinen- und Anlagenbau -----




More information about the Qt-interest-old mailing list