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项目。