当我运行以下函数时,对话框显示所有内容。问题是按钮无法连接。OK 和 Cancel 不响应鼠标点击。
void MainWindow::initializeBOX(){
QDialog dlg;
QVBoxLayout la(&dlg);
QLineEdit ed;
la.addWidget(&ed);
//QDialogButtonBox bb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
//btnbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
la.addWidget(buttonBox);
dlg.setLayout(&la);
if(dlg.exec() == QDialog::Accepted)
{
mTabWidget->setTabText(0, ed.text());
}
}
Run Code Online (Sandbox Code Playgroud)
在运行时,cmd 中的错误显示:没有像 accept() 和 reject() 这样的插槽。
问题:
当在 TreeView 上的每行或列设置 QtstyledItemDelegate 时,我的应用程序崩溃,没有任何进一步的信息。尽管如此,为整个 TreeView 设置 QStyledItemDelegate 的工作在我看来似乎很奇怪。
有谁知道这是否是一个已知的错误,或者我错过了什么
我正在将 PySide 1.1.2 与 Qt 4.8 一起使用(更改版本并不是真正的选择,因为这将是一个很大的开销,因为它是一个也在集群上运行的分布式应用程序,其中版本更新将包含大量开销。 .)
这是一个“最小运行示例”
from PySide import QtGui, QtCore
import sys
import easymodel
class MainWin(QtGui.QMainWindow):
def __init__(self, model, parent=None):
super(MainWin, self).__init__(parent)
treeview = QtGui.QTreeView()
treeview.setItemDelegateForColumn(0,QtGui.QStyledItemDelegate())
treeview.setModel(model)
self.setCentralWidget(treeview)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
root = easymodel.TreeItem(easymodel.ListItemData(["Test","Values","Perhabs a button"]))
model = easymodel.TreeModel(root)
for i in range(1,10):
easymodel.TreeItem(easymodel.ListItemData(["Test","Values","Perhabs a button"]),
parent = root,
index=model.index(root.child_count(),1,
parent = root.index()))
mainWin = MainWin(model)
mainWin.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
作为模型,我使用 github 上提供的 …
现在我像这样加载它们:
if __name__ == '__main__':
app = QApplication(sys.argv)
loader = QUiLoader()
file = QFile('main.ui')
file.open(QFile.ReadOnly)
window = loader.load(file)
file.close()
window.show()
# Here:
window.centralwidget.findChild(QListWidget, 'listWidget').addItems(['Item {0}'.format(x) for x in range(100)])
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
但我认为这很不舒服,有没有其他方法,可能加载整个命名空间或其他什么?
我已经准备了很多关于如何在 python 和 pyqt 中将多个信号连接到同一个事件处理程序的帖子。例如,将多个按钮或组合框连接到同一功能。
许多示例展示了如何使用 QSignalMapper 执行此操作,但是当信号携带参数时不适用,例如 combobox.currentIndexChanged
许多人建议它可以用 lambda 来制作。这是一个干净漂亮的解决方案,我同意,但没有人提到 lambda 创建了一个闭包,其中包含一个引用 - 因此无法删除被引用的对象。你好内存泄漏!
证明:
from PyQt4 import QtGui, QtCore
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
# create and set the layout
lay_main = QtGui.QHBoxLayout()
self.setLayout(lay_main)
# create two comboboxes and connect them to a single handler with lambda
combobox = QtGui.QComboBox()
combobox.addItems('Nol Adyn Dwa Tri'.split())
combobox.currentIndexChanged.connect(lambda ind: self.on_selected('1', ind))
lay_main.addWidget(combobox)
combobox = QtGui.QComboBox()
combobox.addItems('Nol Adyn Dwa Tri'.split())
combobox.currentIndexChanged.connect(lambda ind: self.on_selected('2', ind))
lay_main.addWidget(combobox)
# let the handler show …Run Code Online (Sandbox Code Playgroud) 这很可能是一个重复的问题,但我不得不问它,因为其他答案对我来说没有帮助,因为我是 pyqt 的新手(几天前从 tkinter 转换而来)。
我想知道是否可以连接到这样的小部件事件:
self.lineEdit = QtGui.QLineEdit(self.frame)
self.lineEdit.keyReleaseEvent(lambda: someFunction(QtCore.Qt.Key_A ))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
Run Code Online (Sandbox Code Playgroud)
进而...
def someFunction(event):
print(event)
...
Run Code Online (Sandbox Code Playgroud)
我的问题是如何从另一个小部件绑定到特定事件,并将该事件与类似函数的btn.clicked.connect(function_goes_here).
在 tkinter 它是这样的:
self.Entry.bind("<KeyRelease-a>", lambda event: someFunction(event))
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PyQt开发软件,但是我经常陷入没有调试信息(仅退出代码0xC0000409)的软件崩溃的情况。我正在使用QThread,我写了一个像这样的系统:
class serialThreadC(QThread):
updateOutBox = QtCore.pyqtSignal(str)
updateStatus = QtCore.pyqtSignal(int)
def __init__(self):
super(serialThreadC, self).__init__()
self.ser = False
self.state = 0
self.serialEnabled = False
def run(self):
while True:
if self.state == -3 or self.state == -2:
if self.SerialEnabled:
self.updatePB(20)
elif self.state == 0:
if self.serialEnabled:
self.updatePB(20)
def ConnDisconn(self):
self.serialEnabled = not self.serialEnabled
def updatePB(self, stat):
self.state = stat
self.updateStatus.emit(self.state)
serialThread = serialThreadC()
serialThread.start()
## sw is a QDialog already loaded
serialThread.updateOutBox.connect(sw.updateOutBox)
serialThread.updateStatus.connect(sw.updateStatus)
sw.PB_ConnDisconn.clicked.connect(serialThread.ConnDisconn)
Run Code Online (Sandbox Code Playgroud)
当我serialEnabled在run()或中进行读/写操作时发生崩溃ConnDisconn()。我知道PyQt不是线程安全的,变量的错误处理会导致我的类型崩溃,但是我无法理解我的代码出了什么问题。我的想法(可能是错误的)是所有serialThread方法都在同一线程上执行,即使它们连接到gui(主线程)也是如此。错了吗 …
我设置我的项目委托如下:
COMBOBOX_ITEMS_FRUITS = ['Apple', 'Banana']
COMBOBOX_ITEMS_COLORS = ['Red', 'Green', 'Blue']
self.treeview.setItemDelegateForColumn(COLUMN_A, ComboBoxDelegate(COMBOBOX_ITEMS_FRUITS))
self.treeview.setItemDelegateForColumn(COLUMN_B, ComboBoxDelegate(COMBOBOX_ITEMS_COLORS))
Run Code Online (Sandbox Code Playgroud)
将模型设置为代理模型的源模型后,我的应用程序崩溃但没有抛出任何错误:
self.model_source = treeview_model
self.sf_proxy_model.setSourceModel(self.model_source)
Run Code Online (Sandbox Code Playgroud)
setItemDelegateForColumn当使用sortfilterproxymodel处理源模型时,我似乎只能使用一个.
ComboBoxDelegate 定义如下:
class ComboBoxDelegate(QStyledItemDelegate):
def __init__(self, items):
super(ComboBoxDelegate, self).__init__()
self.items = items
def createEditor(self, parent, option, index):
editor = QComboBox(parent)
editor.setAutoFillBackground(True)
for item in self.items:
editor.addItem(item)
return editor
def setEditorData(self, editor, index):
current_index = editor.findText(index.model().data(index), Qt.MatchExactly)
editor.setCurrentIndex(current_index)
def setModelData(self, editor, model, index):
item_index = model.mapToSource(index)
item = model.sourceModel().item(item_index.row(), 0)
if index.parent().row() == -1 and item.hasChildren():
for row in range(item.rowCount()): …Run Code Online (Sandbox Code Playgroud) 我的问题是在这个链接中回答得很好的问题的扩展:
我在下面发布了答案,当字符串包含单词"ball"时,字符串被过滤掉:
In [3]: df[df['ids'].str.contains("ball")]
Out[3]:
ids vals
0 aball 1
1 bball 2
3 fball 4
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:如果我的数据中有长句,我想识别带有"ball"和"field"字样的字符串怎么办?因此当它们中只有一个出现时,它会丢弃包含单词"ball"或"field"的数据,但保留字符串中包含两个单词的数据.
我已经了解了如何调整布局中小部件之间的空间: Space Between widgets in QVBoxLayout。但是,我想知道如何调整添加到布局的布局之间的空间。
例如:
layout = QVBoxLayout()
h1_layout = QHBoxLayout()
h2_layout = QHBoxLayout()
layout.addLayout(h1_layout)
layout.addLayout(h2_layout)
Run Code Online (Sandbox Code Playgroud)
我希望h1_layout和的内容h2_layout彼此靠近放置,而不是在垂直布局上间隔开。
在PyQt5中使用QPlaintextEdit时,如果按Tab键盘上的按钮,将得到一个等于六个空格大小的制表符空格。但我希望它是四个空格的大小,以便在使用时:
TextEdit.setPlainTextEdit('\t')
Run Code Online (Sandbox Code Playgroud)
我应该缩进制表符空格,该空格一共只要四个空格。
我尝试使用四个空格代替制表符空格,但是随着代码变得越来越冗长,事情变得复杂了。
python ×8
pyqt ×5
pyqt5 ×2
pyside ×2
python-3.x ×2
qt-designer ×2
qtreeview ×2
c++ ×1
events ×1
filtering ×1
indentation ×1
layout ×1
pandas ×1
pycharm ×1
qdialog ×1
qt ×1
qvboxlayout ×1
spacing ×1
uic ×1