2013-09-10 63 views
0

我已經子類的QWidget如下:子類QWidget的不動正確

class myClass : public QWidget 
{ 
public: 
    explicit myClass(QWidget *parent); 
protected: 
    void paintEvent(QPaintEvent *event); 
} 

myWidget::myWidget(QWidget* parent) : QWidget(parent) 
{ 
    setGeometry(10,10,100,100); 
} 

void myWidget::paintEvent(QPaintEvent *event) 
{ 
    QPainter qp(this); 
    QBrush bBlue(QColor::blue); 
    qp.fillRect(geometry(), bBlue); 
} 

我想要的是創造一個藍色背景的大小100,100 10,10 QWidget的放置到QWidget的父母。

我得到的是MyWidget的默認大小,類似於100,50,0,0具有黑色背景(或透明)和藍色矩形,在myWidget中以10,10開始並由myWidget剪輯。

這就像setGeometry在myWidget中移動了一個矩形,而不是myWidget本身。

相當新的Qt和喜歡的上述解釋和修復...

預先感謝您。

Gary。

...這裏是實際代碼:

這是進myWidget

class piTemplateWidget : public QWidget 
{ 

public: 
     explicit piTemplateWidget(QWidget* parent); 

    static QColor* white; 
    static QColor* black; 
    static QColor* lightGrey; 
    static QColor* lightGreen; 

    piTemplate* tplt; 

protected: 
    void paintEvent(QPaintEvent *event); 
}; 


QColor* piTemplateWidget::white = new QColor(15,15,15); 
QColor* piTemplateWidget::black = new QColor(250,250,250); 
QColor* piTemplateWidget::lightGrey = new QColor(100,100,100); 
QColor* piTemplateWidget::lightGreen = new QColor(250,15,250); 

piTemplateWidget::piTemplateWidget(QWidget* parent) : QWidget(parent) 
{ 
    tplt = NULL; 
    move(100,100); 
    resize(300,240); 
} 

void piTemplateWidget::paintEvent(QPaintEvent *event) 
{ 
    QPainter qp(this); 

    QBrush bWhite(*white); 

    qp.fillRect(this->geometry(), bWhite); 

// if (tplt==NULL) 
//  return; 

// tplt->render(&qp); 

} 

...這是父小部件構造函數實例我小部件

piTemplateEdit::piTemplateEdit(QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::piTemplateEdit) 
{ 
    ui->setupUi(this); 

    currentTemplate = NULL; 
    if (piTemplate::templates->count()>0) 
    { 
     currentTemplate = (piTemplate*)piTemplate::templates->atIndex(0); 
    } 
    templateWidget = new piTemplateWidget(this); 
    templateWidget->tplt = currentTemplate; 
} 

...我希望這有助於。

謝謝。

+0

剛添加實際的代碼... –

+0

感謝您的幫助,希望您能看到下面的評論和我出錯的地方... –

回答

0

在構造函數期間設置geometry可能會被父窗口小部件對其調用的show事件覆蓋。

一個共同的主要功能如下所示:

#include <QtGui/QApplication> 
#include "mainwindow.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 
    // w.showMaxmized(); // This line would trump the "setGeometry() call 
         // in the constructor 

    return a.exec(); 
} 

存儲在一個QWidget幾何RECT這裏描述:

http://qt-project.org/doc/qt-4.8/application-windows.html

http://qt-project.org/doc/qt-4.8/qwidget.html#pos-prop

我不會用這內部QWidget設置爲您如何填充您的小部件。如果您確實想要存儲某些設置,請創建一個QRect成員變量並使用該變量。

如果你要填寫你的QWidget的整個盒與彩色你應該嘗試這樣的事:

void myWidget::paintEvent(QPaintEvent *event) 
{ 
    QPainter qp(this); 
    QBrush bBlue(QColor::blue); 
    qp.fillRect(QRect(0,0, this->width(), this->height()), bBlue); 
} 

裏面漆的功能,它們相對於上漆區你在

http://qt-project.org/doc/qt-4.8/qwidget.html#mapTo

就像@LaszloPapp說的,你需要使用resize()move()。在其中任何一個之後投入update()電話並不會有什麼壞處。

此外請務必檢查show()方法及其所有「另請參見」項目。

http://qt-project.org/doc/qt-4.8/qwidget.html#show

http://qt-project.org/doc/qt-4.8/qshowevent.html

如果#include <QShowEvent>,並調用resize()時顯示事件發生時,你可能會好到哪裏去。如果您將此小部件嵌套在另一個小部件中,則應該使用尺寸提示和setFixedSize或正確使用佈局來查看。

http://qt-project.org/doc/qt-4.8/layout.html

希望有所幫助。

+0

嗨phyatt。感謝您的回答。我必須要睡一會兒!原始的setGeometry(10,10,100,100)工作得很好。它是錯誤的fillRect ...正如你所期望的那樣 - 在這裏傳遞的矩形是相對於QWidget客戶區而言的,所以應該位於0,0。這是扔我的東西。我想你回答了我的問題 - 非常感謝和乾杯! –