C++ QT:为什么是我的小部件;“摇晃”;何时调整窗口大小?

C++ QT:为什么是我的小部件;“摇晃”;何时调整窗口大小?,c++,qt,user-interface,qt5,window-managers,C++,Qt,User Interface,Qt5,Window Managers,在我的UI设计中,我希望四个qpushbutton保持在窗口的右侧。因此,我为centralWidget创建了一个HBoxLayout,并向其中添加了一个QWidget和一个VBoxLayout(包含四个按钮)。VBoxLayout的拉伸系数设置为0,这样它将以最小宽度保持在右侧 但是,当我尝试从左侧调整窗口大小时,我的QPushButtons在调整大小时会有点“抖动”,这会带来非常糟糕的外观 那么,这可能是什么原因呢?有什么办法可以避免这种“震动”吗 一个可重复的例子: main window

在我的UI设计中,我希望四个
qpushbutton
保持在窗口的右侧。因此,我为
centralWidget
创建了一个
HBoxLayout
,并向其中添加了一个QWidget和一个
VBoxLayout
(包含四个按钮)。
VBoxLayout
的拉伸系数设置为0,这样它将以最小宽度保持在右侧

但是,当我尝试从左侧调整窗口大小时,我的
QPushButtons
在调整大小时会有点“抖动”,这会带来非常糟糕的外观

那么,这可能是什么原因呢?有什么办法可以避免这种“震动”吗

一个可重复的例子:

main window.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0">
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
      <item>
       <widget class="QWidget" name="widget" native="true"/>
      </item>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item>
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
        </item>
       </layout>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
main window.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"

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

MainWindow::~MainWindow()
{
    delete ui;
}
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
#包括“mainwindow.h”
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
w、 show();
返回a.exec();
}

请提供我的经验,将拉伸因子设置为0会导致很多问题,您可以尝试将
VBoxLayout
的拉伸因子设置为1,将左侧内容的拉伸因子设置为高值,例如8,因此调整整个窗口的大小将导致
VBoxLayout
的宽度发生微小变化,这也必须解决“摇晃”的问题。谢谢。提供了一个可复制的示例。请根据我的经验提供一个示例,将拉伸因子设置为0会导致很多问题,您可以尝试将
VBoxLayout
的拉伸因子设置为1,将左侧内容的拉伸因子设置为高值,例如8,因此,调整整个窗口的大小将导致
VBoxLayout
的宽度发生微小变化,这也必须解决“抖动”问题。谢谢。提供了一个可再现的示例。
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}