1.Qt中的类没有虚析构?由于Qt所有类都是从QObject继承下来的,当父类销毁的时候会自动帮你析构子类,所以不用担心delete父类的时候子类没有被销毁。
2.QFontMetrics::elidedText文本超过一定长度可以使用...
3.事件循环阻止代码继续执行
QEventLoop eventLoop;
QObject::connect(&wnd, SIGNAL(sigClose()), &eventLoop, SLOT(quit()), Qt::DirectConnection);
wnd.show();
eventLoop.exec();
4.QTreeWidget单击展开和收缩
connect(ui->treeRoom, &QTreeWidget::itemPressed,
this
{
if (NULL == item->parent()) {
item->setExpanded(!item->isExpanded());
}
});
5.增加右键菜单
// 右键菜单
ui->treeRoom->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeRoom, &QWidget::customContextMenuRequested,
this
{ onRightClickMenu(); });
6.使用setProperty可以给任何QObject对象传递参数。
7.setWindowFlags指定Qt::Tool | Qt::FramelessWindowHint使其在任务栏没有窗口显示。
8.QTimer实现延时处理和间隔处理
void delayHandle(int msTimeout, const std::function<void()> &func) { QTimer *p = new QTimer(this); p->setSingleShot(true); connect(p, &QTimer::timeout, [func, p] () { p->deleteLater(); func(); }); p->start(msTimeout); } void intervalHandleOnce(int msTime, const std::function<void()> &func) { static QTimer *p = NULL; if (p == NULL) { p = new QTimer(); p->setSingleShot(true); } if (p->interval() != msTime) { p->setInterval(msTime); p->stop(); } if (p->isActive()) { return; } p->start(); if (func) { func(); } } |