简体   繁体   English

Qt5覆盖小部件大小不正确

[英]Qt5 overlay widget not resizing properly

I am trying to design a widget which overlaps another one. 我正在尝试设计与另一个重叠的小部件。 I successfully done it, but I am facing a little problem during the resize event of my QMainWindow . 我成功完成了此操作,但是在QMainWindow的resize事件中遇到了一个小问题。

I created an OverlayView widget, derived from QWidget . 我创建了一个继承自QWidgetOverlayView小部件。 The constructor used is defined as follows: 使用的构造函数定义如下:

 OverlayView::OverlayView(QWidget* widget, const QColor& color, float opacity, OverLayPosition position) : QWidget(nullptr), referenceWidget_(widget), overlayPosition_(position), opacity_(opacity), overlayColor_(color) { overlayRect_ = referenceWidget_->rect(); overlayRect_.setWidth(overlayRect_.width() * 0.05f); setAutoFillBackground(false); setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); } 

The referenceWidget_ class member is a pointer to a widget which is used to get its size. referenceWidget_类成员是指向用于获取其大小的窗口小部件的指针。 In my project, I used my QMainWindow . 在我的项目中,我使用了QMainWindow

I have just made a little experiment to see if the OverlayView widget was resizing correctly: 我做了一个小实验,看看OverlayView小部件的大小是否正确:

 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QPlainTextEdit* pte = new QPlainTextEdit(this); pte->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); setCentralWidget(pte); overlayView_ = new OverlayView(this, QColor(0x33, 0x99, 0x38, 120)); overlayView_->setParent(this); connect(this, SIGNAL(updateOverlaySize(const QRect&)), overlayView_, SLOT(updateOverlaySize(const QRect&))); } 

The OverlayView widget is connected to the updateOverlaySize slot through the updateOverlaySize signal of my QMainWindow used as follows: OverlayView小部件通过QMainWindowupdateOverlaySize信号连接到updateOverlaySize插槽,其使用方式如下:

 void MainWindow::resizeEvent(QResizeEvent* event) { QMainWindow::resizeEvent(event); emit updateOverlaySize(rect()); } 

The updateOverlaySize slot of the OverlayView is defined like this: OverlayViewupdateOverlaySize插槽的定义如下:

 void OverlayView::updateOverlaySize(const QRect& rect) { overlayRect_ = rect; } 

Just for testing purposes, the overlay occupy the QMainWindow . 仅出于测试目的,覆盖层占用QMainWindow

Finally, the overlay is drawn by redefining the paintEvent of the OverlayView : 最后,通过重新定义OverlayViewpaintEvent来绘制覆盖图:

 void OverlayView::paintEvent(QPaintEvent*) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(Qt::NoPen); painter.fillRect(overlayRect_, overlayColor_); } 

Here is the result: 结果如下:

预期结果

Everything seems to be fine... 一切似乎都很好...

不是预期的结果

...mmmh not really. ...不是真的。 The overlay stops to be resized when the size of the QMainWindow exceed a certain value. QMainWindow的大小超过某个值时,覆盖层停止调整大小。

The QRect object received from the updateOverlaySize slot shows correct values. updateOverlaySize插槽收到的QRect对象显示正确的值。

If someone could help me to fix this. 如果有人可以帮助我解决此问题。 Thanks for your answers. 感谢您的回答。

I will answer my own question. 我会回答我自己的问题。

I just ommited to update the geometry of my widget. 我只是忽略了更新小部件的几何形状。 According to Qt doc, the geometry property do this: 根据Qt doc,geometry属性可以做到这一点:

This property holds the geometry of the widget relative to its parent and excluding the window frame. 此属性保存窗口小部件相对于其父窗口的几何形状,但不包括窗口框架。

When changing the geometry, the widget, if visible, receives a move event (moveEvent()) and/or a resize event (resizeEvent()) immediately. 更改几何时,小部件(如果可见)会立即接收移动事件(moveEvent())和/或调整大小事件(resizeEvent())。 If the widget is not currently visible, it is guaranteed to receive appropriate events before it is shown. 如果窗口小部件当前不可见,则可以保证在显示窗口小部件之前会接收到适当的事件。

The size component is adjusted if it lies outside the range defined by minimumSize() and maximumSize(). 如果大小分量超出由minimumSize()和maximumSize()定义的范围,则将对其进行调整。

The overlayRect_ class member was updated successfully but not the size of my widget. overlayRect_类成员已成功更新,但未更新我的窗口小部件的大小。 This seems to be a problem. 这似乎是一个问题。

Updating the geometry in the updateOverlaySize slot fixed this: 更新updateOverlaySize插槽中的几何updateOverlaySize此问题:

 void OverlayView::updateOverlaySize(const QRect& rect) { overlayRect_ = rect; setgeometry(rect); } 

Now, the size() method of the OverlayView widget is returning its real size. 现在, OverlayView小部件的size()方法将返回其实际大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM