QTreeWidget中MainWindow窗体中布局器不起作用

Qt系列文章目录

文章目录

  • Qt系列文章目录
  • 前言
  • 一、错误原因?
  • 二、修改代码
  • 总结

前言

QTreeWidget中MainWindow窗体中布局器不起作用
这个示例会创建一个包含QTreeWidget的QMainWindow窗体,QTreeWidget将填满整个窗体。当你调整窗体大小时,QTreeWidget会自动调整其大小以填充可用空间。你可以根据需要使用其他类型的布局器,如QHBoxLayout、QGridLayout等。


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 



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;
    QTreeWidget* m_picTree;
    QGridLayout* m_grid;


};

#endif // MAINWINDOW_H


#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include 
#include 


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

//    QWidget *centralWidget = new QWidget(this);

    m_picTree = new QTreeWidget(this);
    //打开右键菜单属性
    m_picTree->setContextMenuPolicy(Qt::CustomContextMenu);
    m_picTree->setColumnCount(1);//设置有1列
    m_picTree->setHeaderLabels(QStringList() << u8"彩C" );


    QVector<QString> nameVec;
    for (int i = 0; i < 10; ++i) {
        QString temp;
        nameVec.push_back(QString("%1").arg(i));
    }

    QTreeWidgetItem *topItem = new QTreeWidgetItem(m_picTree);
    topItem->setText(0, u8"英雄");

    for (int i = 0; i < 10; ++i) {
        QTreeWidgetItem* item = new QTreeWidgetItem(topItem);
        item->setText(0, nameVec.at(i));
    }

    m_grid = new QGridLayout;
    m_grid->addWidget(m_picTree);
    this->setLayout(m_grid);
//    centralWidget->setLayout(m_grid);

//    this->setCentralWidget(centralWidget);

}

MainWindow::~MainWindow()
{
    delete ui;
}



运行结果
QTreeWidget中MainWindow窗体中布局器不起作用_第1张图片
界面中的QTreeWidget控件挤成一团,就算设置: m_picTree->expandAll();也没有效果,这个树控件不会自动展开

一、错误原因?

在这种情况下,问题是将QGridLayout直接设置为MainWindow的布局。QMainWindow已经有一个内置的布局,你应该将QTreeWidget设置为中央窗口,而不是更改QMainWindow的布局。

二、修改代码

加入:

QWidget *centralWidget = new QWidget(this);
this->setCentralWidget(centralWidget);

#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include 
#include 


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

    QWidget *centralWidget = new QWidget(this);

    m_picTree = new QTreeWidget(this);
    //打开右键菜单属性
    m_picTree->setContextMenuPolicy(Qt::CustomContextMenu);
    m_picTree->setColumnCount(1);//设置有1列
    m_picTree->setHeaderLabels(QStringList() << u8"彩C" );
    m_picTree->expandAll();

    QVector<QString> nameVec;
    for (int i = 0; i < 10; ++i) {
        QString temp;
        nameVec.push_back(QString("%1").arg(i));
    }

    QTreeWidgetItem *topItem = new QTreeWidgetItem(m_picTree);
    topItem->setText(0, u8"英雄");

    for (int i = 0; i < 10; ++i) {
        QTreeWidgetItem* item = new QTreeWidgetItem(topItem);
        item->setText(0, nameVec.at(i));
    }

    m_grid = new QGridLayout;
    m_grid->addWidget(m_picTree);
    this->setLayout(m_grid);
    centralWidget->setLayout(m_grid);

    this->setCentralWidget(centralWidget);

}

MainWindow::~MainWindow()
{
    delete ui;
}



总结

`修改后的运行结果QTreeWidget中MainWindow窗体中布局器不起作用_第2张图片

你可能感兴趣的:(Qt,QTreeWidget,QGridLayout,Qt布局器)