QT手写显示控件如QDockWidget,QTabWidget,QGroupBox组合

在 Qt 中,你可以在 QDockWidget 中添加一个 QTabWidget,然后在其中一个选项卡上添加多个 QGroupBox。每个 QGroupBox 中包含几个 QPushButton、一个 QLabel 和两个 QLineEdit,并使用 QGridLayout 布局来排列这些控件。

以下是一个完整的示例代码,展示了如何实现这个布局:

cpp 复制代码
#include <QApplication>
#include <QMainWindow>
#include <QDockWidget>
#include <QTabWidget>
#include <QWidget>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>

class MainWindow : public QMainWindow {
public:
    MainWindow() {
        // Create central widget and set it as the central widget for the main window
        QWidget *centralWidget = new QWidget(this);
        setCentralWidget(centralWidget);

        // Create a QDockWidget
        QDockWidget *dockWidget = new QDockWidget("Dock Widget", this);
        addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

        // Create a QTabWidget and set it to the dock widget
        QTabWidget *tabWidget = new QTabWidget(dockWidget);
        dockWidget->setWidget(tabWidget);

        // Create the first tab
        QWidget *firstTab = new QWidget(tabWidget);
        tabWidget->addTab(firstTab, "First Tab");

        // Create a vertical layout for the first tab
        QVBoxLayout *vboxLayout = new QVBoxLayout(firstTab);

        // Add four QGroupBox widgets to the vertical layout
        for (int i = 0; i < 4; ++i) {
            QGroupBox *groupBox = new QGroupBox(QString("Group Box %1").arg(i + 1), firstTab);
            vboxLayout->addWidget(groupBox);
            
            // Create a QGridLayout for the group box
            QGridLayout *gridLayout = new QGridLayout(groupBox);
            
            // Add buttons, label, and line edits to the grid layout
            for (int j = 0; j < 5; ++j) {
                QPushButton *button = new QPushButton(QString("Button %1").arg(j + 1), groupBox);
                gridLayout->addWidget(button, j, 0);  // Buttons in the first column
            }
            
            QLabel *label = new QLabel(QString("Label %1").arg(i + 1), groupBox);
            gridLayout->addWidget(label, 0, 1);  // Label in the first row, second column
            
            QLineEdit *lineEdit1 = new QLineEdit(groupBox);
            gridLayout->addWidget(lineEdit1, 1, 1);  // Line Edit 1 in the second row, second column
            
            QLineEdit *lineEdit2 = new QLineEdit(groupBox);
            gridLayout->addWidget(lineEdit2, 2, 1);  // Line Edit 2 in the third row, second column
        }

        setWindowTitle("QDockWidget with QTabWidget Example");
        resize(800, 600);
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MainWindow mainWindow;
    mainWindow.show();

    return app.exec();
}

代码解释

  1. 创建主窗口MainWindow 继承自 QMainWindow,是主应用程序窗口。

  2. 设置中央小部件 :创建一个 QWidget 并将其设置为主窗口的中央小部件。

  3. 创建 QDockWidget

    • 使用 QDockWidget 创建一个停靠窗口,并将其添加到主窗口的左侧区域。
  4. 创建 QTabWidget

    • QDockWidget 中创建一个 QTabWidget 并设置为停靠窗口的内容。
  5. 创建第一个选项卡

    • 创建一个 QWidget 作为第一个选项卡,并将其添加到 QTabWidget
  6. 配置布局

    • 在选项卡上创建一个 QVBoxLayout,用于垂直排列 QGroupBox
    • 创建四个 QGroupBox,并将它们添加到垂直布局中。
    • 每个 QGroupBox 中使用 QGridLayout 进行内部布局。
  7. 添加控件到每个 QGroupBox

    • 每个 QGroupBox 内部包含五个 QPushButton、一个 QLabel 和两个 QLineEdit
    • 使用 QGridLayout 将这些控件按网格排列:按钮在第一列,标签和输入框在第二列的不同位置。
相关推荐
天若有情6736 分钟前
打破思维定式!C++参数设计新范式:让结构体替代传统参数列表
java·开发语言·c++
斯特凡今天也很帅9 分钟前
python测试SFTP连通性
开发语言·python·ftp
sunywz11 分钟前
【JVM】(4)JVM对象创建与内存分配机制深度剖析
开发语言·jvm·python
亲爱的非洲野猪12 分钟前
从ReentrantLock到AQS:深入解析Java并发锁的实现哲学
java·开发语言
星火开发设计12 分钟前
C++ set 全面解析与实战指南
开发语言·c++·学习·青少年编程·编程·set·知识
沛沛老爹28 分钟前
Web开发者进阶AI:Agent Skills-深度迭代处理架构——从递归函数到智能决策引擎
java·开发语言·人工智能·科技·架构·企业开发·发展趋势
Good_Starry36 分钟前
Java——正则表达式
java·开发语言·正则表达式
二哈喇子!40 分钟前
前端HTML、CSS、JS、VUE 汇总
开发语言·前端
欧洵.43 分钟前
Java.基于UDP协议的核心内容
java·开发语言·udp
情缘晓梦.1 小时前
C语言数据存储
c语言·开发语言