QT5 QTreeview显示问题
昨天写了一个treewidget,怎么也不在mainwidow中显示。
原因:没有将QTreeWidget添加到mainwidow中。
解决方法:
将QTreeWidget添加到一个具有布局的widget中,再将该widget添加到一个QDockWidget中(QDockWidget在mainwindow中)。
代码如下:
m_pDockWidget = new QDockWidget(this);//m_pDockWidget 为QDockWidget
m_pDockWidget->setMinimumWidth(240);//设置大小
m_pDockWidget->setMaximumWidth(240);
addDockWidget(Qt::LeftDockWidgetArea, m_pDockWidget);//将m_pDockWidget添加到左侧区域
m_pDockWidget->setFeatures(QDockWidget::DockWidgetClosable);//设置特性,可关闭
m_pDockWidget->setFeatures(QDockWidget::DockWidgetFloatable);//停靠部件可以脱离主窗口,并作为独立窗口浮动.。
m_pDockWidget->setFeatures(QDockWidget::DockWidgetMovable);//自由移动
QWidget *pDocLeftUpWidget = new QWidget();
m_pDockWidget->setWidget(pDocLeftUpWidget);
QVBoxLayout *pLeftVBoxLayout = new QVBoxLayout(pDocLeftUpWidget);//水平布局
m_pLeftCRTTree = new CptTreeView();//CptTreeView继承TreeWidget
m_pLeftCRTTree->setMaximumHeight(300);
m_pLeftCRTTree->setMinimumHeight(200);
m_pLeftCRTTree->setStyleSheet("QHeaderView::section{background:rgb(63,175,223); color:white;font-size:10pt;}"
"QTreeView::item:selected {background:rgb(63,175,223); color: white;}"
"QTreeView::item:hover {background:rgb(155,205,255); color: white;}"
);//设置样式
//设置图标
m_pLeftCRTTree->headerItem()->setIcon(0, QIcon(":/File/img/leftUpCptTreeIcon1.png"));
//将DockWidget上的黑色边框去掉
QWidget * titleBarWidget = m_pDockWidget->titleBarWidget();
QWidget* lEmptyWidget = new QWidget();
m_pDockWidget->setTitleBarWidget(lEmptyWidget);
delete titleBarWidget;
//将m_pLeftCRTTree添加到布局中
pLeftVBoxLayout->addWidget(m_pLeftCRTTree);
|