Qt Widgets & Window Settings

1. Button properties

(一)Qt::WA_TransparentForMouseEvents

Realize the mouse penetrating function, which is similar to the effect of "taking things from the air and hitting cattle from mountains".

//qwidget.h
void setAttribute(Qt::WidgetAttribute, bool on = true);

When enabled, this property disables delivery of mouse events to the widget and its children. Mouse events are passed to other widgets as if the widget and its children did not exist in the widget hierarchy; mouse clicks and other events effectively "pass through" them. This property is disabled by default.

1. Usage

theButton->setAttribute(Qt::WA_TransparentForMouseEvents, true);

The transparent area of ​​the current window does not respond to mouse events:

setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground, true);

If the current window is opaque but needs to achieve mouse penetration, pay attention to the sequence of codes:

// 一定要先设置鼠标穿透,否则无法穿透
setAttribute(Qt::WA_TransparentForMouseEvents, true);
 
setWindowFlag(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground, true);

Examples of other application scenarios:
1. It is forbidden to respond to mouse click events, but it needs to respond to signals at the code level.
2. If necessary, compared with setting the usability of controls, setting properties is more subtle.
3. Some transparent forms (or Shaped interface) allows the mouse to operate the window behind this window interface

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓ 

2. Examples

 //按钮上显示的文字
QLabel * label = new QLabel;
label->setParent(this);
label->setFixedSize(menuBtn->width(),menuBtn->height());
label->setText(QString::number(i+1));
label->move(25 + (i%4)*70 , 130+ (i/4)*70);

//设置label上的文字对齐方式 水平居中 和垂直居中
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

 //鼠标事件穿透
label->setAttribute(Qt::WA_TransparentForMouseEvents,true);

 

Two, window properties

(1) Qt::WA_DeleteOnClose

1 Introduction

When the user closes a main window, the default behavior is to hide it, so it remains in memory. For a large number of main windows, it will cause certain problems.

The solution is to set the property of Qt::WA_DeleteOnClose in the constructor:

setAttribute(Qt::WA_DeleteOnClose);

So if we create a window through new in the program, we can set the Qt::WA_DeleteOnClose property to the window. In this way, when the window is closed, Qt can automatically reclaim the resources occupied by the window, so that invalid resources can be reclaimed in time, which is beneficial to saving memory space.

2. Regarding the wild pointer problem caused by Qt::WA_DeleteOnClose

When setAttribute(QT::WA_DeleteOnClose,true) is set, the destroy signal will be triggered, which can be manually set to nullptr

if(dialog==null)
{
  dialog=new Dialog()
  //注意,如果使用了setAttribute(QT::WA_DeleteOnClose,true);则不能再将其加入到对象树,即不能通过父类this去创建,否则会调用析构函数释放该资源,
  //而setAttribute(QT::WA_DeleteOnClose,true);后,窗口在销毁时也会释放自身资源,导致异常!
  dialog->setAttribute(QT::WA_DeleteOnClose,true);
  connect(dialog, &Dialog::destroyed, this, [=](){
    dialog= nullptr;
    });
}
dialog.show(),

(2) Qt widget & window settings

1 Introduction

Firstly, a variable bool mouseTracking is introduced , which stores a value indicating whether the widget tracks the mouse or not. Its value can be set through the function setMouseTracking() , and its value can be obtained through the function hasMouseTracking() .

  • setMouseTracking(true) : Mouse tracking is enabled, even if the mouse button is not pressed, the widget will receive mouse movement events . mouseMoveEvent(QMouseEvent *event) is triggered whenever the mouse moves .
  • setMouseTracking(false) : mouse tracking is disabled, the widget must also receive mouse movement events when the mouse button is pressed, and then mouseMoveEvent(QMouseEvent *event) will be triggered .

2. Example of use

// 初始配置
setWindowFlags(Qt::WindowCloseButtonHint);//Qt.WindowCloseButtonHint    窗口只有一个关闭按钮
setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setMouseTracking(true);//这里
setWindowTitle(QString("欧陆战争"));
setWindowIcon(QPixmap(MAIN_SCENE_ICON_PATH));

(三)w->setFixedSize(w->width(),w->height());

The difference between calling w->setFixedSize(w->width(), w->height()) before w->show() and calling after w->show()

  • If w->setFixedSize(w->width(),w->height()) is called before w->show(), then w->width() and w->height() obtained at this time are the default value.
  • But if w->setFixedSize(w->width(), w->height()) is called after w->show(), the w->width() and w->height() obtained at this time are widgets The appropriate value is automatically adjusted according to the size of the child control.

1.

w->setFixedSize(w->width(),w->height());

w->show();

The phenomenon is as follows:

 

2.

w->show();

w->setFixedSize(w->width(),w->height());

The phenomenon is as follows:

 

The article is transferred from the blog garden (ImreW): Qt widgets & window settings - ImreW - Blog Garden

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓ 

Guess you like

Origin blog.csdn.net/QtCompany/article/details/132173888