我想从任意长度的列表中选择任意数量的项目.下拉列表(QComboBox)不允许检查项目.可检查项目列表会因很多项目而变得笨拙.
我在User Experience SE子网站中发现了这个问题,这个答案似乎是最适合我需求的解决方案.它有很多名称,作为回答备注中的评论:双列表,累加器,列表构建器,TwoListSelection ......
上面链接的答案中显示的OpenFaces.org版本:

但是我在Qt中找不到实现.我应该自己实现它还是Qt中有可用的实现?有推荐的方法吗?
我一直在尝试找到一种方法来用任何颜色(比如说红色)填充 QGraphicsRectItem 的实例,但我未能找到如何完成这个简单任务的明确方法,如果您能提供一些提示,那就太好了、链接等
多谢
我想检查本地目录中是否存在图像,如果没有,则加载默认图像。我通过使用找到了一种锻炼:
Image {
id: image
source: source1
onStatusChanged: {
if ( (image.status == Image.Error) && source !== default_source ) {
source = default_source
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我更愿意在收到错误之前测试文件是否存在。在尝试加载 file_paths/url/images 之前,有没有更简洁的方法来测试它们?
谢谢!
例如,我正在寻找一种在多个QML文件之间共享只读值的简单方法。可以说我有一个label元素:
Label {
id: titleLabel
text: listView.currentItem ? listView.currentItem.text : "IDEAL Networks"
font.pixelSize: 20
elide: Label.ElideRight
horizontalAlignment: Qt.AlignLeft
verticalAlignment: Qt.AlignVCenter
Layout.fillWidth: true
color: red;
padding: {
left: 14
}
}
Run Code Online (Sandbox Code Playgroud)
该color和padding值需要在其他QML文件和同一文件的其他领域使用。
除了重新键入red和在多个位置14之外,还有一种方法可以创建包含这些值的共享库,从而使以后更容易进行全局更新?
*更新*
我已按照此处的说明进行操作:http : //doc.qt.io/qt-5/qtqml-modules-qmldir.html
但是,当导入自定义CustomStyles 1.0模块时,出现错误-未安装模块“ CustomStyles”。
//Style.qml with custom singleton type definition
pragma Singleton
import QtQuick 2.0
QtObject {
property int textSize: 20
property color textColor: "green"
}
// qmldir declaring the singleton type
module CustomStyles
singleton Style …Run Code Online (Sandbox Code Playgroud) 这是来自 Kivy 相对布局的文档。 https://kivy.org/docs/api-kivy.uix.relativelayout.html
相对布局:此布局允许您为子项设置相对坐标。如果您想要绝对定位,请使用 FloatLayout。relativeLayout 类的行为与常规 FloatLayout 类似,只是它的子窗口小部件相对于布局定位。当将position=(0,0)的widget添加到RelativeLayout中时,当RelativeLayout的位置改变时,子widget也会移动。子窗口部件坐标保持为 (0,0),因为它们始终相对于父布局。
我发现 Floatlayout 也做了同样的事情。事实上,Floatlayout和RelativeLayout都支持绝对和相对定位,具体取决于是否使用pos_hint或pos。
实际上,无论绝对定位和相对定位如何,当布局位置发生变化时,小部件都会移动。
我正在使用Qt Framework for desktop的应用程序.由于我删除了每个窗口装饰,我必须实现主窗口,以便在用户单击并移动鼠标时接收移动事件.
我尝试了以下代码,但我不满意.我想知道是否有更好的方式来更优雅.
QPoint* mouseOffset; //global variable that hold distance of the cursor from
the top left corner of the window.
void ArianaApplication::mouseMoveEvent(QMouseEvent* event)
{
move(event->screenPos().x() - mouseOffset->x(),
event->screenPos().y() - mouseOffset->y());
}
void ArianaApplication::mousePressEvent(QMouseEvent*)
{
mouseOffset = new QPoint(QCursor::pos().x() - pos().x(),
QCursor::pos().y() - pos().y());
}
Run Code Online (Sandbox Code Playgroud)
你能给我别的建议吗?
当我尝试从使用PyQt5 Designer创建的qtextedit中获取文本时,出现错误或“ Python停止工作”,并且脚本自动停止。我尝试了多种解决方案,但没有任何效果。我必须将文本从qtextedit分配给变量,以检查进程是否运行。那是PyQt5生成的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import psutil
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(221, 157)
MainWindow.setMinimumSize(QtCore.QSize(221, 157))
MainWindow.setMaximumSize(QtCore.QSize(221, 157))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.cercaprocesso = QtWidgets.QPushButton(self.centralwidget)
self.cercaprocesso.setGeometry(QtCore.QRect(0, 80, 221, 31))
font = QtGui.QFont()
font.setFamily("MS Shell Dlg 2")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.cercaprocesso.setFont(font)
self.cercaprocesso.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 221, 40))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.textbox = QtWidgets.QTextEdit(self.centralwidget)
self.textbox.setGeometry(QtCore.QRect(30, 40, 161, 31))
self.textbox.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.textbox.setObjectName("textEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 221, 21))
self.menubar.setObjectName("menubar") …Run Code Online (Sandbox Code Playgroud) 我有一个名为“Cell.qml”的自定义 QML 项目,我想将其动态插入到我的根窗口中并使其可见。我也尝试更改 z 属性,但我无法修复它,该对象仍然不可见(即使 root->dumpObjectTree() 的输出也表明该对象作为根窗口的子窗口存在)
这是执行代码后的结果

主程序
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject *root = engine.rootObjects()[0];
QQmlComponent component(&engine, "qrc:/Cell.qml");
if (component.isReady()){
QObject *object = component.create();
object->setParent(root);
engine.setObjectOwnership(object, engine.CppOwnership);
}
root->dumpObjectTree();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
主文件
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
Run Code Online (Sandbox Code Playgroud)
细胞.qml
import QtQuick 2.0
import QtQuick.Controls 2.3
Item{
property string txt: "" …Run Code Online (Sandbox Code Playgroud) 我需要通过单击QPushButton我的应用程序中的 a(通过clicked)来生成一个自定义弹出输入窗口。它需要从不同类型的用户那里获取多个输入,然后将它们返回给主窗口应用程序内的调用函数。我发现内置函数QInputDialog可以为单个特定输入执行此操作,但我无法弄清楚如何在一次要求多个不同类型输入的弹出窗口的情况下执行此操作(最好在设计的窗口中)在 Qt 设计器中)。有谁知道如何做到这一点?
import sys
import os
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic
path = os.path.dirname(__file__) #uic paths from itself, not the active dir, so path needed
qtCreatorFile = "NAME.ui" #Ui file name, from QtDesigner
Ui_MainWindow, QtBaseClass = uic.loadUiType(path + qtCreatorFile) #process through pyuic
class MyApp(QMainWindow, Ui_MainWindow): #gui class
def __init__(self):
#Set up the gui via Qt
super(MyApp, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.add_button.clicked.connect(self.add_row) #add_button is QPushButton
def add_row(self):
data1, …Run Code Online (Sandbox Code Playgroud) 这是我的文本编辑器代码,
import sys
from collections import Counter
from PyQt5.QtCore import pyqtRemoveInputHook
from PyQt5.QtWidgets import QMainWindow, QApplication, QAction
from PyQt5 import uic
Ui_MainWindow, QtBaseClass = uic.loadUiType('EditorUI.ui')
class MyApp(QMainWindow):
def __init__(self):
self.newLines = 1
super(MyApp, self).__init__(None)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
# New Action
self.newAction = QAction('&New', self)
self.newAction.triggered.connect(self.NewCall)
fileMenu.addAction(self.newAction)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.textEdit.setViewportMargins(35, 0, 0, 0)
self.ui.textEdit.textChanged.connect(self.NumNewLines)
def NumNewLines(self): # Returns Number of lines Of text written in the editor
self.newLines = Counter(self.ui.textEdit.toPlainText())['\n']+1
def NewCall(self):
print('new')
if __name__ == …Run Code Online (Sandbox Code Playgroud)