去除QToolBar左侧三个小点
调用 setMovable(false);
移除样式
//移除原有样式
QWidget *widget = new QWidget;
style()->unpolish(widget );
//重新设置新的该控件的样式。
style()->polish(widget);
QTreeWidget水平滚动条
QTreeWidget 设置水平滚动条
treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
treeWidget->header()->setStretchLastSection(false);
QTableWidgetItem存在黑色边框
将QTableWidget以及QTableWidgetItem的边框样式都设置为none的情况下。仍就存在黑色边框。
解决办法:QTableWidget 对象调用setShowGrid(false)
即可
QPushButton文字左对齐
QPushButton button = new QPushButton;
button->setStyleSheet("text-align:left;");
QDateTimeEdit中设置图标
查看QDateTimeEdit 源码可以看到 中间日期编辑部分是一个QLineEdit,名称为 qt_spinbox_lineedit。因此通过findChild可以拿到这个QLineEdit;此时通过设置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);
}
Qt中获得屏幕的宽和高
QApplication::desktop()->width() ;
QApplication::desktop()->height() ;
多块屏幕也可
win7阴影边框
setWindowFlags(windowFlags() | Qt::NoDropShadowWindowHint);
忽略鼠标事件
setAttribute(Qt::WA_TransparentForMouseEvents);
注意:无法忽略鼠标滚轮事件;
QComboBo设置样式无效
加上 setView( new QListView());
即可
QComboBox设置文字为空
直接调用 setCurrentText("")
无效。可以通过 setCurrentIndex(-1)
实现
Qt 信号连接报警告问题
今天在使用QTimer的时候,连接其timerout信号。然后在控制台总是输出提示信息。
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 *))
然后我就直接看QTimer的源码。发现
Q_SIGNALS:
void timeout(QPrivateSignal);
我发现timeout多了一个参数,以为是参数问题,可是我记得它是没有参数的,就很奇怪。后面检查发现,是因为我在调用connect的时候,timer对象的内存申请放在了connect后面,导致是一个空的对象。把new 放在了connect前面,就解决了。
Qt程序运行目录与程序环境目录获取
QString exeDir= QApplication::applicationDirPath();//exe运行目录
QString dirPath = QDir::currentPath().toStdString();//程序运行环境目录
Qt截取屏幕并保存为图片
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
此问题是因为未曾正确处理线程退出从而引起。需要在主线程中添加如下代码:
_trimeThread 为子类化QThread的对象;
//quit
_trimeThread->quit();//退出线程
_trimeThread->wait();//等待主线程退出 join()
QTableWidget的item编辑完后获取其内容
在QTableWidget的item编辑完毕之后,要是内容改变会发送
void QTableWidget::itemChanged(QTableWidgetItem *item)
信号,关联此信号然后即可获取item的内容
QMenu之二级菜单
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删除布局中的子控件以及子布局
Qt::Popup flags问题
QAction设置选中
QAction *action = new QAction(nullptr);
action->setCheckable(true);
action->setChecked(true);
多个QAction设置选中互斥
将其添加到QActionGroup中即可互斥
如下 代码,actionTwo 被选中
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);