0, make good use of CSS, Qt control beautification

Blog.csdn.net/libaineu200…

 

1. Qt Splitter designer attributes have two options at the bottom:

Opaqueresize and childrenCollapsible

If checked, the child window will be redrawn when the splitter is dragged; If not checked, redraw will not be done.

 

2, QTreeView sets row background color (color) alternation

Why it’s useful: QTreeView’s background is blank by default, which is visually unappealing.

To achieve the effect: If you want to alternate the background color of the row, change the color of the next row

Involving functions: voidQTreeView: : setAlternatingRowColors (boolenable);

Sample code:

centertreeview->setAlternatingRowColors(true);

The specific row background color RGB can be implemented via QSS.

QTableView {background: RGB (220235255); Alternate background - color: RGB (238238242); color:black; }Copy the code

 

To implement mouseMoveEvent, add setMouseTrack(true) to the constructor to get the listener event directly. If setMouseTrack(false) is used, mouseMove listens for event responses only when the mouse is pressed.

 

4, QTableView column width adaptive text width

this->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

 

5. Emit dataChanged(leftTop, rightBottom) using Qtableview with XxxModel derived from QAbstractTableModel. And then the data in the TableView will be updated

 

6. QMainWindow is not a normal Widget, and the setCentralWidget is required:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {/* error 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); */ /* 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); */ // The correct way 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); }Copy the code

 

We can also delete it:

QWidget *p = takeCentralWidget();

    if (p)

{

delete p;

}

Source github’s QDockWidget_VSStudioMode project.