小编ekh*_*oro的帖子

使用 QTreeView 行中的文本对齐方式错误

我有一个QTreeView(带有PyQt4),使用下面的代码自定义和调整大小的图标,但“大小”列显示错误的对齐/位置,如下所示:

在此输入图像描述

        self.ui.treeView.setIconSize(QtCore.QSize(30,30))

        fileSystemModel = QtGui.QFileSystemModel(self.ui.treeView)
        custonIconProvider = CustomIconsProvider()
        fileSystemModel.setIconProvider(custonIconProvider)

        self.ui.treeView.setModel(fileSystemModel)
        self.ui.treeView.setRootIndex(fileSystemModel.setRootPath(forlderPath))

        self.ui.treeView.setColumnWidth(0, 250)
        self.ui.treeView.setColumnWidth(1, 70)
        self.ui.treeView.setColumnWidth(2, 70)
Run Code Online (Sandbox Code Playgroud)

我已经搜索了http://pyqt.sourceforge.net/Docs/PyQt4/qtreeview.html文档以寻找可能的修复方法,但找不到任何明显的内容。

python pyqt text-alignment pyqt4 qtreeview

5
推荐指数
1
解决办法
1990
查看次数

如何在QGraphicsScene上绘图而不缩放画笔宽度?

我正在尝试绘制 QGraphicScene 数据,该数据可能会根据情况而变化几个数量级。由于我使用的笔是装饰性的,因此我希望视图与数据的大小无关。但我得到的是这样的:

带噪声的正弦波乘以 50000:

规模50000

带噪声的正弦波乘以 50:

规模 50

但是,如果我放大这些图中的任何一个(两个图的缩放量相同),我最终会达到两个图像看起来相同的水平:

缩放比例50000 在此输入图像描述

这里发生了什么?为什么数据值变大了,笔的宽度就变了?为什么放大后缩放比例消失了?

重现此内容的代码如下。左键单击图可放大,右键单击可缩小。

import sys

from PyQt4 import QtGui as QG
from PyQt4 import QtCore as QC

import numpy as n

class ZoomView(QG.QGraphicsView):
    """Zoomable QGraphicsView"""
    def mouseReleaseEvent(self,event):
        if event.button() == QC.Qt.LeftButton:
            self.scale(1.5,1)
        elif event.button() == QC.Qt.RightButton:
            self.scale(1/1.5,1)

class MainUI(QG.QDialog):
    def __init__(self, parent=None):
        super(MainUI, self).__init__(parent)

        layout = QG.QVBoxLayout()
        self.setLayout(layout)
        button_layout = QG.QHBoxLayout()
        pb3 = QG.QPushButton('add plot')
        button_layout.addWidget(pb3)
        layout.addLayout(button_layout)
        pb3.clicked.connect(self.scene_maker_singleshot)
        scene = QG.QGraphicsScene()
        view = ZoomView(self)
        view.setTransformationAnchor(QG.QGraphicsView.AnchorUnderMouse)
        view.setRenderHint(QG.QPainter.Antialiasing)
        layout.addWidget(view)
        view.setScene(scene)

        self.view = view
        self.scene = …
Run Code Online (Sandbox Code Playgroud)

qt qt4 pyqt pyqt4

5
推荐指数
1
解决办法
1685
查看次数

pyqt动态生成QMenu动作并连接

还在学习pyqt如何工作.我想动态生成customContextMenu并与函数连接.到目前为止,我得到以下,但连接部分不工作?

import sys
from PyQt4 import QtGui, QtCore

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

    # create button
    self.button = QtGui.QPushButton("test button", self)       
    self.button.resize(100, 30)

    # set button context menu policy
    self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    self.connect(self.button, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
    self.popMenu = QtGui.QMenu(self)

    def on_context_menu(self, point):
        self.popMenu.clear()

        #some test list for test
        testItems = ['itemA', 'itemB', 'itemC']
        for item in testItems:
            action = self.btn_selectPyFilterPopMenu.addAction("Selected %s" % item)
            self.connect(action,QtCore.SIGNAL("triggered()"),self,QtCore.SLOT("printItem('%s')" % item))    
        self.popMenu.exec_(self.button.mapToGlobal(point))

    @pyqtSlot(str)
    def printItem(self, item):
        print item

def main():
    app = QtGui.QApplication(sys.argv)
    form = …
Run Code Online (Sandbox Code Playgroud)

python signals-slots pyqt4 qmenu

5
推荐指数
1
解决办法
7734
查看次数

QTableView中的搜索/查找功能

我有一个QWidget,里面有一个QTableView.我需要在表的第一列有一个查找功能,所以当我点击Ctrl + F时,会弹出一个查找对话框.

class Widget(QWidget):
    def __init__(self,md,parent=None):
        QWidget.__init__(self,parent)
        layout=QVBoxLayout(self)

        # initially construct the visible table
        tv = QTableView()
        # uncomment this if the last column shall cover the rest
        tv.horizontalHeader().setStretchLastSection(True)
        tv.show()

        # set black grid lines
        self.setStyleSheet("gridline-color: rgb(39, 42, 49)")

        # construct the Qt model belonging to the visible table
        model = NvmQtModel(md)
        tv.setModel(model)
        tv.resizeRowsToContents()
        tv.resizeColumnsToContents()

        # set the shortcut ctrl+F for find in menu
        shortcut = QShortcut(QKeySequence('Ctrl+f'), self)
        shortcut.activated.connect(self.handleFind)

        # delegate for decimal
        delegate = NvmDelegate()
        tv.setItemDelegate(delegate)
        self.setGeometry(200,200,600,600) # …
Run Code Online (Sandbox Code Playgroud)

python search qt pyqt qtableview

5
推荐指数
1
解决办法
2040
查看次数

PyQt:在运行时向滚动区域添加小部件

我试图在运行时通过按下按钮来添加新的小部件(在下面的示例中我使用标签)。这里是例子:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget(QWidget):

    def __init__(self, parent= None):
        super(Widget, self).__init__()

        btn_new = QPushButton("Append new label")
        self.connect(btn_new, SIGNAL('clicked()'), self.add_new_label)

        #Container Widget       
        self.widget = QWidget()
        #Layout of Container Widget
        layout = QVBoxLayout(self)
        for _ in range(20):
            label = QLabel("test")
            layout.addWidget(label)
        self.widget.setLayout(layout)

        #Scroll Area Properties
        scroll = QScrollArea()
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scroll.setWidgetResizable(False)
        scroll.setWidget(self.widget)

        #Scroll Area Layer add
        vLayout = QVBoxLayout(self)
        vLayout.addWidget(btn_new)
        vLayout.addWidget(scroll)
        self.setLayout(vLayout)

    def add_new_label(self):
        label = QLabel("new")
        self.widget.layout().addWidget(label)


if __name__ == '__main__':
    app = …
Run Code Online (Sandbox Code Playgroud)

qt resize pyqt qlabel qscrollarea

5
推荐指数
1
解决办法
1万
查看次数

PyQt:从 QDesigner 包装对话框和连接按钮

我开始学习 Python 和 PyQt。目前,我正在解决一个关于连接信号和插槽的非常基本的问题,使用从 QDesigner 生成的对话框表单。我想从 QDialog 连接一个按钮。该代码不会产生错误。正如预期的那样,对话框显示出来。但是点击按钮没有任何反应。

或者,我尝试将代码表单Ui_Dialog直接包含在我的目标类中Testdialog。然后连接正常工作。似乎我在从Ui_DialogtoTestdialog和/或我想要执行对话框的方式继承属性时犯了一个错误。

我的主程序如下所示:

from __future__ import unicode_literals
import sys

from PyQt4 import *
from PyQt4 import QtGui
from PyQt4.QtCore import SIGNAL, QObject

import UI_Test



class Testdialog(QtGui.QDialog, UI_Test.Ui_Dialog):
    def __init__(self,parent=None):
        super(Testdialog, self).__init__(parent)
        self.setupUi(self)
        print("Connect buttons") # gives the expected output

        self.connect(self.pushButton_Ok, SIGNAL("clicked()"), self.clickedOk)
        self.connect(self.pushButton_Cancel, SIGNAL("clicked()"), self.clickedCancel)

        # Alternativly I have tríed the following without improvement:
        # self.pushButton_Ok.clicked.connect(self.clickedOk)
        # QObject.connect(self.pushButton_Cancel, SIGNAL("clicked()"), self.clickedCancel)


    def clickedCancel(self):
        print ("Cancel")  # …
Run Code Online (Sandbox Code Playgroud)

python pyqt signals-slots qpushbutton pyuic

5
推荐指数
1
解决办法
1082
查看次数

PyQt5 弹出窗口

我正在尝试使用 PyQt5 构建一个应用程序,当QListWidget双击a 中的项目时,该应用程序有一个辅助窗口“弹出” 。

这是代码:

import sys
from PyQt5.QtWidgets import QWidget, QListWidget, QListWidgetItem, QLabel, QPushButton, QApplication


class exampleWidget(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        listWidget = QListWidget(self)
        listWidget.itemDoubleClicked.connect(self.buildExamplePopup)

        names = ["Jack", "Chris", "Joey", "Kim", "Duncan"]

        for n in names:
            QListWidgetItem(n, listWidget)

        self.setGeometry(100, 100, 100, 100)
        self.show()

    @staticmethod
    def buildExamplePopup(item):
        name = item.text()
        exPopup = examplePopup(name)
        exPopup.setGeometry(100, 200, 100, 100)
        exPopup.show()


class examplePopup(QWidget):
    def __init__(self, name):
        super().__init__()

        self.name = name

        self.initUI()

    def initUI(self):
        lblName = QLabel(self.name, self)

if …
Run Code Online (Sandbox Code Playgroud)

popup python-3.x pyqt5

5
推荐指数
1
解决办法
2万
查看次数

为什么PyCharm使用双反斜杠来表示逃逸?

例如,我写了一个普通字符串和另一个"异常"字符串,如下所示:

在此输入图像描述

现在我调试它,发现在调试工具中,"异常"字符串将显示如下:

在此输入图像描述

这是问题:

为什么PyCharm显示双反斜杠而不是单反斜杠?众所周知,\'意味着'.有什么伎俩吗?

python string escaping backslash pycharm

5
推荐指数
1
解决办法
1142
查看次数

在Qt5中无法从QWheelEvent获取pixelDelta

我从Qt4升级到Qt5(PyQt,具体而言)和QWheelEvent为我打破 - 它返回空pixelDelta(),但阶段是2(默认).我在Win 7上,所以关于阶段的警告不适用于我.当我运行此代码时:

from PyQt5 import QtWidgets


class Q(QtWidgets.QLabel):

    def wheelEvent(self, event):
        print(event.pixelDelta())

app = QtWidgets.QApplication([])
w = Q()
w.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud)

滚动打印'没有坐标的PyQt5.QtCore.QPoint()'.我能做什么?

python events mousewheel qt5 pyqt5

5
推荐指数
1
解决办法
1116
查看次数

VirtualEnv未正确设置路径,无法使用已安装的模块

简短版本:我如何让PyQt4的configure.py使用Virtual Env中安装的SIP版本?

长版本:我知道这个问题的变化在这里被问过一百万次,但我似乎无法找到答案.我正在尝试在虚拟环境(VE)中安装SIP和PyQt4.我无法将其安装到主系统,因为它是一台工作计算机.我们有一个旧版本的PyQt所以我不能只是从网站包复制.

我在我的VE中安装了SIP(configure.py --incdir,make,make install)但是当我在PyQt4上运行configure时,我得到错误:错误:此版本的PyQt需要SIP v4.19.0或更高版本.我安装了4.19.2版.当运行sipconfig时,它告诉我它仍在使用系统版本,即使激活了VE.如何告诉configure.py PyQt使用VE中安装的软件包?

谢谢大家!

编辑:从它的外观来看,似乎我的VE没有从正确的位置拉蟒蛇库,即使VE被激活.我添加了一行来激活将site-packages和bin dirs的路径和site-packages附加到pythonpath而没有成功.它仍然没有找到正确的库.

python virtualenv pyqt4 python-sip

5
推荐指数
1
解决办法
456
查看次数