0、利用好CSS,實作Qt控件美化 https://blog.csdn.net/libaineu2004/article/details/19621831
1、Qt splitter設計師屬性最下方有兩個選項:
opaqueresize和childrenCollapsible
勾選之後,則分割器拖動時子視窗會重繪;不勾選則不重繪。
2、QTreeView設定行背景色(顔色) 交替
使用原因:QTreeView的背景預設是一片空白的,這樣在視覺上不美觀。
達到效果:如果要達到行背景色交替改變,隔一行顔色變化一下
涉及函數:voidQTreeView::setAlternatingRowColors ( boolenable);
示例代碼:
centertreeview->setAlternatingRowColors(true);
具體的行背景顔色RGB可以通過QSS實作。
QTableView {
background: rgb(220,235,255);
alternate-background-color: rgb(238,238,242);
color:black;
}
3、要想實作mouseMoveEvent,則需要在構造函數中添加setMouseTrack(true),直接得到監聽事件。若是setMouseTrack(false),隻有滑鼠按下才會有mouseMove監聽事件響應。
4、QTableView列寬自适應文字寬度
this->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
5、用了Qtableview 搭配 從QAbstractTableModel 繼承的XxxModel, 使用内部标準信号 emit dataChanged( leftTop, rightBottom ); 後,tableview的資料會更新
6、QMainWindow不是普通的Widget,需要注意細節setCentralWidget是必須的:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/*錯誤的方式
QPushButton *btn1 = new QPushButton("hello", this);
QPushButton *btn2 = new QPushButton("world", this);
QVBoxLayout *lay = new QVBoxLayout(this);
lay->addWidget(btn1);
lay->addWidget(btn2);
this->setLayout(lay);*/
/*錯誤的方式
QPushButton *btn1 = new QPushButton("hello", this->centralWidget());
QPushButton *btn2 = new QPushButton("world", this->centralWidget());
QVBoxLayout *lay = new QVBoxLayout(this->centralWidget());
lay->addWidget(btn1);
lay->addWidget(btn2);
this->centralWidget()->setLayout(lay);*/
/*錯誤的方式
QPushButton *btn1 = new QPushButton("hello");
QPushButton *btn2 = new QPushButton("world");
QVBoxLayout *lay = new QVBoxLayout();
lay->addWidget(btn1);
lay->addWidget(btn2);
this->centralWidget()->setLayout(lay);*/
//正确的方式
QWidget *w = new QWidget(this);
QPushButton *btn1 = new QPushButton("hello", w);
QPushButton *btn2 = new QPushButton("world", w);
QVBoxLayout *lay1 = new QVBoxLayout(w);
lay1->addWidget(btn1);
lay1->addWidget(btn2);
w->setLayout(lay1);
setCentralWidget(w);
}

我們也可以删除它:
//删除中央窗體
QWidget *p = takeCentralWidget();
if (p)
{
delete p;
}
來源github的QDockWidget_VSStudioMode項目。