Qt备忘录

程序启动时去掉dos窗口

1
#pragma comment(linker, "/subsystem:windows /entry:mainCRTStartup")

DoModal

1
2
3
4
	DlgTst dlg(this);
	int result = dlg.exec();
	if (QDialog::Accepted == result)
		QMessageBox::information(this, "title", "done.");

Modelless

1
2
3
	DlgTst* dlg2 = new DlgTst(this);
//	dlg2->setModal(Qt::ApplicationModal);
	dlg2->show();

嵌入子对话框

1
	setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);

保持对话框前置,类似于MFC的SetOwner

1
	setWindowFlags(Qt::Dialog);

PostMessage
signal slot connect的默认行为类似于SendMessage,如果要实现PostMessage的行为,connect时加参数Qt::QueuedConnection。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class MainWindow : public QMainWindow
{
	...
private slots:
	void on_pushButton_clicked();
	void on_pushButton_2_clicked();
signals:
	void mySignal1();
};
 
MainWindow::MainWindow(QWidget* parent)
{
	...
	connect(this, SIGNAL(mySignal1()), SLOT(on_pushButton_2_clicked()), Qt::QueuedConnection);
}
 
void MainWindow::on_pushButton_clicked()
{
	QMessageBox::information(this, "title", "on_pushButton_clicked 1");
	emit mySignal1();
	QMessageBox::information(this, "title", "on_pushButton_clicked 2");
}
 
void MainWindow::on_pushButton_2_clicked()
{
	QMessageBox::information(this, "title", "B");
}

安装完qt-vs-tools-msvc2013-2.0.0.vsix后,重启vs2013,工程项目右键菜单上qt功能选项为灰色无效,怎么办?
打开.vcxproj修改

1
<Keyword>Win32Proj</Keyword>

1
<Keyword>Qt4VSv1.0</Keyword>

再重启vs,在工程上右键菜单上点击“Convert Project to Qt VS Tools Project”。

生成要翻译的语言文件

1
2
3
4
lupdate hello.pro -ts lang.ts
# or in cmake
add_custom_target(tr COMMAND ${Qt5_LUPDATE_EXECUTABLE} -target-language en ${Srcs} -ts "${CMAKE_CURRENT_SOURCE_DIR}/lang.ts")
lrelease lang.ts -qm lang.qm

窗口上画矩形

1
2
3
4
5
6
7
void DlgTst01::paintEvent(QPaintEvent *e)
{
	QPainter painter(this);
	painter.setBrush(Qt::red);
	painter.setPen(Qt::red);
	painter.drawRect(QRect(0, 0, this->width(), this->height()));
}

支持c++20

1
2
# CMakeLists.txt
set(CMAKE_CXX_STANDARD 20)

禁止背景重绘

1
ui->widgetVideo->setUpdatesEnabled(false);

皮肤skin
https://blog.csdn.net/weixin_42126427
http://t.zoukankan.com/luoxiang-p-13528745.html

无边框窗体的拖动和改变大小
https://www.cnblogs.com/warmlight/p/12841968.html
https://blog.csdn.net/zhushentian/article/details/82021838

Qt Style Sheets Reference
export QT_DEBUG_PLUGINS=1

refer to:
https://blog.csdn.net/qq_24127015/article/details/95118124
https://blog.csdn.net/zyx4843/article/details/50682212
https://blog.csdn.net/ermzdy2/article/details/99692954
https://blog.csdn.net/aaa123524457/article/details/80582978
http://www.myexception.cn/qt/1119616.html
https://blog.csdn.net/hl1hl/article/details/85244451
https://stackoverflow.com/questions/48187569/qt-translation-file-is-removed-on-make-clean-using-cmake