【问题标题】:Subclassed QWidget does not move correctly子类化的 QWidget 不能正确移动
【发布时间】:2013-09-14 17:53:52
【问题描述】:

我将 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);
}

我想要的是创建一个蓝色背景的 QWidget 放置在 QWidget 父级的 10,10 大小为 100,100 处。

我得到的是 myWidget 的默认大小,例如 0,0 处的 100,50,黑色背景(或透明)和蓝色矩形,在 myWidget 中从 10,10 开始并被 myWidget 剪裁。

这就像 setGeometry 在 myWidget 中移动了一个矩形,而不是 myWidget 本身。

对 Qt 相当陌生,希望能得到上述解释和修复...

提前谢谢你。

加里。

...这是实际代码:

这是我的小部件

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;
}

...我希望这会有所帮助。

谢谢。

【问题讨论】:

  • 刚刚添加了实际代码...
  • 感谢您的帮助,希望您能看到下面的 cmets 以及我哪里出错了...

标签: qt


【解决方案1】:

在构造函数期间设置geometry 可能会被父窗口小部件调用的show 事件覆盖。

一个常见的 main 函数可以是这样的:

#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 中的几何矩形:

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 &lt;QShowEvent&gt;,并在演出活动发生时致电resize(),您可能会很高兴。如果您将此小部件嵌套在另一个小部件中,您应该考虑使用尺寸提示和setFixedSize 或正确使用布局。

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

希望对您有所帮助。

【讨论】:

  • 嗨 phyatt。感谢您的回答。我必须要睡一会儿!原来的 setGeometry(10,10,100,100) 工作得很好。这是错误的填充矩形......正如你所渴望的那样 - 传入的矩形是相对于 QWidget 客户区域的,因此应该位于 0,0。这就是让我失望的原因。我猜你回答了我的问题 - 非常感谢和干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
相关资源
最近更新 更多