QFileDialog select folder
QString dir = QFileDialog::getExistingDirectory();
Remove the three small dots on the left side of QToolBar
Call setMovable(false);
Remove style
//移除原有样式
QWidget *widget = new QWidget;
style()->unpolish(widget );
//重新设置新的该控件的样式。
style()->polish(widget);
QTreeWidget horizontal scroll bar
QTreeWidget sets the horizontal scroll bar
treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
treeWidget->header()->setStretchLastSection(false);
QTableWidgetItem has a black border
When the border styles of QTableWidget and QTableWidgetItem are set to none. There is still a black border.
Solution: QTableWidget object calls setShowGrid(false)
to
QPushButton text left aligned
QPushButton button = new QPushButton;
button->setStyleSheet("text-align:left;");
Set icon in QDateTimeEdit
Looking at the QDateTimeEdit source code, you can see that the middle date editing part is a QLineEdit named qt_spinbox_lineedit . Therefore, you can get this QLineEdit through findChild; at this time, you can set QAction.
QDateTimeEdit *dateTimeEdit = new QDateTimeEdit;
QLineEdit *lineEdit = dateTimeEdit->findChild<QLineEdit*>("qt_spinbox_lineedit");
if(lineEdit)
{
QAction *actionIcon = new QAction(lineEdit);
actionIcon->setIcon(QIcon("icon.png"));
lineEdit->addAction(actionIcon,QLineEdit::ActionPosition::LeadingPosition);
}
Get the width and height of the screen in Qt
QApplication::desktop()->width() ;
QApplication::desktop()->height() ;
Multiple screens are also available
win7 shadow border
setWindowFlags(windowFlags() | Qt::NoDropShadowWindowHint);
Ignore mouse events
setAttribute(Qt::WA_TransparentForMouseEvents);
Note: Mouse wheel events cannot be ignored;
QComboBo setting style is invalid
Plus setView( new QListView());
you can
QComboBox sets the text to be empty
Direct call setCurrentText("")
invalid. Can be setCurrentIndex(-1)
achieved
Qt signal connection reports warning problem
When using QTimer today, connect its timerout signal. Then the prompt message is always output on the console.
Warning: QObject::connect: invalid null parameter --File:(kernel\qobject.cpp:46
74, class QMetaObject::Connection __cdecl QObjectPrivate::connectImpl(const clas
s QObject *,int,const class QObject *,void **,class QtPrivate::QSlotObjectBase *
,enum Qt::ConnectionType,const int *,const struct QMetaObject *))
Then I just look at the source code of QTimer directly. Find
Q_SIGNALS:
void timeout(QPrivateSignal);
I found that timeout has one more parameter, thinking it is a parameter problem, but I remember it has no parameters, which is very strange. Later inspection found that it was because when I called connect, the memory application of the timer object was placed after the connect, which resulted in an empty object. Put new in front of connect, and it's solved.
Qt program running directory and program environment directory acquisition
QString exeDir= QApplication::applicationDirPath();//exe运行目录
QString dirPath = QDir::currentPath().toStdString();//程序运行环境目录
Qt captures the screen and saves it as a picture
auto desktopWidget = QApplication::desktop();
QScreen * pqscreen = QGuiApplication::primaryScreen();
QPixmap pixmap = pqscreen->grabWindow(desktopWidget->winId(), 0, 0, desktopWidget->width(), desktopWidget->height());
pixmap.save("data/a.png");
QTread Destroryed while thread is still running
This problem is caused because the thread exit is not handled properly. The following code needs to be added to the main thread:
_trimeThread 为子类化QThread的对象;
//quit
_trimeThread->quit();//退出线程
_trimeThread->wait();//等待主线程退出 join()
Get its content after editing the item of QTableWidget
After the item of QTableWidget is edited, if the content changes, it will be sent
void QTableWidget::itemChanged(QTableWidgetItem *item)
Signal, associate this signal and then you can get the content of the item
QMenu secondary menu
QMenu *menu = new QMenu(nullptr);
QAction *actionChildMenu = new QAction(nullptr);
actionChildMenu->setText("child menu");
QMenu *menuChild = new QMenu(nullptr);//二级菜单
QAction *actionChildOne = new QAction(nullptr);
actionChildOne->setText("actionChildOne");
QAction *actionChildTwo = new QAction(nullptr);
actionChildTwo->setText("actionChildTwo");
QAction *actionChildThree = new QAction(nullptr);//二级菜单项
actionChildTwo->setText("actionChildTwo");
menuChild->addAction(actionChildOne);
menuChild->addAction(actionChildTwo);
menuChild->addAction(actionChildThree);
menu->addAction(actionChildMenu);
actionChildMenu->setMenu(menuChild);
QWidget deletes sub-controls and sub-layouts in the layout
Qt::Popup flags problem
QAction setting selected
QAction *action = new QAction(nullptr);
action->setCheckable(true);
action->setChecked(true);
Multiple QAction settings are mutually exclusive when selected
Add it to QActionGroup to be mutually exclusive. The
following code, actionTwo is selected
QActionGroup *actionGroup = new QActionGroup(nullptr);
QAction *actionOne = new QAction(nullptr);
QAction *actionTwo = new QAction(nullptr);
QAction *actionThree = new QAction(nullptr);
actionGroup->addAction(actionOne);
actionGroup->addAction(actionTwo);
actionGroup->addAction(actionThree);
actionOne->setCheckable(true);
actionTwo->setCheckable(true);
actionThree->setCheckable(true);
actionTwo->setChecked(true);