我有一个简单的PyQt4应用程序(请参阅下面的代码),它揭示了下一个问题:如果我从QLineEdit中选择文本并将其复制到剪贴板,那么我只能在我的应用程序运行时将其粘贴到另一个应用程序.似乎在退出时PyQt应用程序清除了剪贴板,因此我无法在应用程序关闭后粘贴文本.
我该怎么做才能避免这个问题?
PyQt 4.4.3 @ Python 2.5 @ Windows XP.此效果也在PyQt 4.5+和Linux上得到证实.
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
edit = QtGui.QLineEdit()
edit.setText('foo bar')
edit.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud) 我正在使用QtCore.QThread(来自PyQt4).
要记录,我还使用以下格式化程序:
logging.Formatter('%(levelname)-8s %(asctime)s %(threadName)-15s %(message)s')
生成的日志是:
DEBUG 2012-10-01 03:59:31,479 Dummy-3 my_message
我的问题是我想更明确地知道哪个线程正在记录... Dummy-3对我来说不是最明确的名字....
有没有办法将一个名称设置为一个QtCore.QThread可供日志记录模块使用的名称(作为LogRecord属性),以使日志更有意义?
谢谢 !
我有这个代码:
myEdit = QLineEdit()
myQFormLayout.addRow("myLabelText", myEdit)
Run Code Online (Sandbox Code Playgroud)
现在我必须通过引用删除行myEdit:
myQformLayout.removeRow(myEdit)
Run Code Online (Sandbox Code Playgroud)
但是没有API.我可以使用.takeAt(),但我怎么能得到这个论点?如何找到标签索引或索引myEdit?
我有两个问题:
我想知道这是否是在单列树视图上进行搜索/过滤的正确方法。我觉得我的很多复制/粘贴可能包含不必要的内容。子类中的代码QSortFilterProxyModel和方法中的代码都search_text_changed需要吗?我觉得不需要正则表达式,因为我将过滤代理设置为忽略大小写。
我怎样才能做到这一点,以便当用户双击树视图项目时,信号会发出一个字符串列表,其中包含所单击项目的字符串及其递归的所有祖先?例如,如果我双击“Birds”,它将返回['Birds','Animals'];如果我双击“动物”,它只会返回['Animals'].
import os, sys
from PySide import QtCore, QtGui
tags = {
"Animals": [
"Birds",
"Various"
],
"Brick": [
"Blocks",
"Special"
],
"Manmade": [
"Air Conditioners",
"Audio Equipment"
],
"Food": [
"Fruit",
"Grains and Seeds"
]
}
class SearchProxyModel(QtGui.QSortFilterProxyModel):
def __init__(self, parent=None):
super(SearchProxyModel, self).__init__(parent)
self.text = ''
# Recursive search
def _accept_index(self, idx):
if idx.isValid():
text = idx.data(role=QtCore.Qt.DisplayRole).lower()
condition = text.find(self.text) >= 0
if condition:
return True
for childnum in …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用python3和Qt4从现有窗口调用一个新窗口.
我使用Qt Designer(主应用程序和另一个)创建了两个窗口,我将Qt Designer生成的.ui文件转换为.py脚本 - 但我似乎无法从主应用程序创建新窗口.
我试过这样做:
############### MAIN APPLICATION SCRIPT ################
from PyQt4 import QtCore, QtGui
import v2
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(194, 101)
self.button1 = QtGui.QPushButton(Form)
self.button1.setGeometry(QtCore.QRect(50, 30, 99, 23))
self.button1.setObjectName(_fromUtf8("button1"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.button1.setText(QtGui.QApplication.translate("Form", "Ventana", None, QtGui.QApplication.UnicodeUTF8))
self.button1.connect(self.button1, QtCore.SIGNAL(_fromUtf8("clicked()")), self.mbutton1)
def mbutton1(self):
v2.main()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form() …Run Code Online (Sandbox Code Playgroud) 如何在没有继承的情况下处理鼠标事件,用例可以描述如下:
假设我想让QLabel对象变为handel MouseMoveEvent,教程中的方式通常会影响我们创建一个从QLabel继承的新类.但是我可以使用lambda表达式来处理事件而不需要继承
ql = QLabel()
ql.mouseMoveEvent = lambda e : print e.x(), e.y()
Run Code Online (Sandbox Code Playgroud)
所以我不需要编写整个类,只需使用简单的lambda表达式来实现一些简单的事件.
QGroupBox一旦它高于400px,我试图使我的可滚动.QGroupBox使用for循环生成其中的内容.这是如何完成的一个例子.
mygroupbox = QtGui.QGroupBox('this is my groupbox')
myform = QtGui.QFormLayout()
labellist = []
combolist = []
for i in range(val):
labellist.append(QtGui.QLabel('mylabel'))
combolist.append(QtGui.QComboBox())
myform.addRow(labellist[i],combolist[i])
mygroupbox.setLayout(myform)
Run Code Online (Sandbox Code Playgroud)
由于值val取决于某些其他因素,myform因此无法确定布局大小.为了解决这个问题,我添加了QScrollableArea这样的内容.
scroll = QtGui.QScrollableArea()
scroll.setWidget(mygroupbox)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎对groupbox没有任何影响.没有滚动条的迹象.我错过了什么吗?
我有一个简单的表单,包括一些组合,标签,按钮和QTextEdit.
我尝试用keyPressEvent捕获enter或return键,但由于某种原因我无法做到.然而,我也使用的ESC键被识别.
这是一段代码:
def keyPressEvent(self, e):
print e.key()
if e.key() == QtCore.Qt.Key_Return:
self.created.setText('return')
if e.key() == QtCore.Qt.Key_Enter:
self.created.setText('enter')
if e.key() == QtCore.Qt.Key_Escape:
self.cmbEdit = not(self.cmbEdit)
if self.cmbEdit:
Run Code Online (Sandbox Code Playgroud)
等等...
我错过了什么吗?
我使用Python 3.4与Pyside 1.2.4和PyQt 4.8.7,当我尝试将信号连接到插槽时,它说:
'PySide.QtCore.Signal'对象没有属性'connect'
我正在使用MVC:
模型:
from PySide.QtCore import Signal
class Model(object):
def __init__(self):
self.updateProgress = Signal(int)
Run Code Online (Sandbox Code Playgroud)
控制器:
class Controller(QWidget):
"""
MVC Pattern: Represents the controller class
"""
def __init__(self, parent=None):
super().__init__(parent)
self.model = Model()
self.model.updateProgress.connect(self.setProgress)
Run Code Online (Sandbox Code Playgroud)
当我在Pycharm中查找Class时,通过按住CTRL并单击Signal类,它看起来如下所示:
class Signal(object):
""" Signal """
def __call__(self, *args, **kwargs): # real signature unknown
""" Call self as a function. """
pass
def __getitem__(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass
def __init__(self, *args, **kwargs): # real signature unknown …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Gulp任务生成bundle.js文件
var gulp = require('gulp'),
gutil = require('gulp-util'),
wrench = require('wrench'),
conf = require('./webpack.config'),
webpack = require('webpack'),
WebpackDevServer = require('webpack-dev-server');
// Webpack
gulp.task('wp', function() {
return gulp.src('./assets/scripts/entry.js')
.pipe(webpack(conf))
.pipe(gulp.dest('./assets/build1'));
});
Run Code Online (Sandbox Code Playgroud)
但是,TypeError: dest.on is not a function当我尝试运行它时,我得到了:
这是文件夹目录: