一、Qt Widgets 问题交流
二、Qt Quick 问题交流
1.Dialog 或 Popup 弹框位置默认可以设置到 Window 可视范围外
(测试版本,Qt5.12和Qt5.15)
Dialog 或者 Popup 的位置默认是可以设置到 Window 可视范围之外的,这就导致弹框显示不完整。如图:
但是给弹框设置 margin 大于等于 0 之后就会被限定在 Window 范围内,这样我们在自定义弹框拖动的时候就不用判断是否超出边界了。
代码如下:
import QtQuick 2.12
import QtQml 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("GongJianBo")
MyDialog {
id: dialog
}
MouseArea {
anchors.fill: parent
onClicked: dialog.open()
}
}
import QtQuick 2.12
import QtQuick.Controls 2.12
Popup {
id: control
width: 500
height: 350
padding: 0
margins: 0
modal: true
closePolicy: Popup.CloseOnEscape
//背景
Rectangle {
id: bg_area
anchors.fill: parent
border.color: "black"
//用于拖拽对话框
MouseArea {
id: bg_mouse
anchors.fill: parent
property point clickPos: Qt.point(0,0)
property bool dragMoving: false
onPressed: {
dragMoving = true;
clickPos = Qt.point(mouseX,mouseY);
}
onReleased: {
dragMoving = false;
}
onPositionChanged: {
if(!dragMoving){
return;
}
control.x += mouseX-clickPos.x;
control.y += mouseY-clickPos.y;
}
}
Button {
anchors.centerIn: parent
text: "close"
onClicked: control.close()
}
}
}
三、其他
1.环境变量设置 QML2_IMPORT_PATH 后,与之不同版本的 QML 程序启动失败
如环境变量设置 Qt5.15:
但是打包的程序是 Qt5.12 等其他版本,会导致 QML 组件不能正常加载:
即使设置了 qt.conf 文件的导入路径,还是不能正常加载。
可以在代码中设置 QML2_IMPORT_PATH 环境变量:
qputenv("QML2_IMPORT_PATH","");
QApplication app(argc, argv);
或者设置 QQmlEngine 的 setImportPathList:
QQmlApplicationEngine engine;
QStringList qml_path;
//Qt6 location接口改名为了path
qml_path.push_front(QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath););
engine.setImportPathList(qml_path);
这样就能覆盖掉环境变量中不同版本的 QML 组件路径。
2.Qt Remote Object source 端多线程 emit 信号,client 只能触发第一次示例工程:https://github.com/gongjianbo/MyTestCode/tree/master/Qt/QtRemoteObjects
将 source 服务端每次都 new 一个新的线程来处理任务,结束之后在该线程中 emit 信号,但 client 客户端关联该信号却只能触发第一次。
//信号测试
connect(ui->btnSignal,&QPushButton::clicked,[this]{
std::thread th([this](){
emit source.dataChanged("server dataChanged");
});
th.detach();
});
可将信号触发放到一个固定的线程中:
//信号测试
connect(ui->btnSignal,&QPushButton::clicked,[this]{
std::thread th([this](){
QMetaObject::invokeMethod(&source,[=]{
emit source.dataChanged("server dataChanged");
},Qt::QueuedConnection);
});
th.detach();
});