小编ekh*_*oro的帖子

如何在 PyQt4 中使用 setRowHeight 和 resizeRowToContents 调整行大小?

我在表格视图中正确调整行大小时遇到​​一个小问题。我有一个垂直标题,没有水平标题。我试过:

self.Popup.table.setModel(notesTableModel(datainput))
self.Popup.table.horizontalHeader().setVisible(False)
self.Popup.table.verticalHeader().setFixedWidth(200)
for n in xrange(self.Popup.table.model().columnCount()):
    self.Popup.table.setColumnWidth(n,150)
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但是当我尝试时:

 for n in xrange(self.Popup.table.model().rowCount()):
     self.Popup.table.setRowHeight(n,100)
Run Code Online (Sandbox Code Playgroud)

或者

for n in xrange(self.Popup.table.model().rowCount()):
     self.Popup.table.resizeRowToContents(n)
Run Code Online (Sandbox Code Playgroud)

即使文本超过单元格的长度,也不会调整行的大小。

如何强制行适合数据?

python resize qtableview pyqt4

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

PyQt5:QMessageBox启动后消失

当我调用错误消息一(请参阅代码中的注释)时,该消息很快出现然后消失。但是,如果我调用错误消息二,它就会出现,并且只有在我单击“确定”按钮时才会消失。

如何修复它以使错误消息一像错误消息二一样工作?

    try:
        connection = pymysql.connect(host = 'localhost',
            user = 'root',
            db = 'Telephon Register',
            cursorclass = pymysql.cursors.DictCursor)  
        cur = connection.cursor()

        if number!= "":
            cur.execute("SELECT Number FROM formen WHERE Telephonebook = " + self.number.text() )
            result = cur.fetchone()

            if len(result) == 0:
                cur.execute("INSERT INTO formen VALUES(" + self.number.text())  
                connection.commit()
            else:
                print("The number " + number+ " already exists.")
        else:
            print("You have not typed a number!")
            msg = QMessageBox()  #EXCEPTION MESSAGE ONE
            msg.setIcon(2)
            msg.setText("Some Text")
            msg.setInformativeText("Some informative text")
            msg.setWindowTitle("Error")
            msg.show()

        connection.close() …
Run Code Online (Sandbox Code Playgroud)

python pyqt qmessagebox pyqt5

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

如何向 QTreeView 行添加按钮

我已经得到了具有选项卡和树视图的代码。我希望树视图中的每一行都有一些小按钮。如何向树视图的行添加按钮?

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

class tabdemo(QTabWidget):
    def __init__(self, parent=None):
      super(tabdemo, self).__init__(parent)
      self.setGeometry(300, 300, 500, 300)
      self.tab1 = QWidget()
      self.tab2 = QWidget()
      self.tab3 = QWidget()


      self.addTab(self.tab1, "Tab 1")
      self.addTab(self.tab2, "Tab 2")
      self.addTab(self.tab3, "Tab 3")
      self.tab1UI()
      self.tab2UI()
      self.tab3UI()
      self.setWindowTitle("tab demo")

    def tab3UI(self):
      layout = QFormLayout()
      layout.addRow("Name", QLineEdit())
      layout.addRow("Address", QLineEdit())
      self.setTabText(0, "Contact Details")
      self.tab3.setLayout(layout)

    def tab2UI(self):
      layout = QFormLayout()
      sex = QHBoxLayout()
      sex.addWidget(QRadioButton("Male"))
      sex.addWidget(QRadioButton("Female"))
      layout.addRow(QLabel("Sex"), sex)
      layout.addRow("Date of Birth", QLineEdit())
      self.setTabText(1, "Personal Details")
      #self.button.clicked.connect(self.handleButton)
      self.tab2.setLayout(layout)



    def …
Run Code Online (Sandbox Code Playgroud)

python qt pyqt pyqt4 qtreeview

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

PyQt5:对象没有属性“exec_”,有两个主窗口

我是 PyQt 的新手,所以当我创建 UI 文件时,我只是复制了一个 Mainwindow (mainfile.ui) 并将其更改为生成另一个 UI 文件 (Intro.ui)。我知道这不是创建 UI 文件的好方法,因为它总是给出错误:object has no attribute 'exec_'.

这是代码:

MainFile = "mainfile.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(MainFile)

FileIntro = "Intro.ui" 

Ui_WindowIntro,_ = uic.loadUiType(FileIntro)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.ButtonIntro.clicked.connect(self.OpenWindowIntro)
    def OpenWindowIntro(self):
        s = WindowIntro()
        s.show()
        s.exec_() #here is the problem.

class WindowIntro(QtWidgets.QMainWindow, Ui_WindowIntro):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_WindowIntro.__init__(self)
        self.setupUi(self)
        #close the window
        self.Button2.clicked.connect(self.Close)

    def Close(self):
        self.close()

if __name__ == "__main__":

    app = 0 # if not the core …
Run Code Online (Sandbox Code Playgroud)

python pyqt exec qt-designer pyuic

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

(PyQt) 如何重置整个 QTextEdit 的 CharFormat?

我在 QTextEdit 中的几个单词上使用 mergeCharFormat,以突出显示它们。像这样的东西:

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

def drawGUI():
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    w.setGeometry(200, 200, 200, 50)
    editBox = QtGui.QTextEdit(w)
    text = 'Hello stack overflow, this is a test and tish is a misspelled word'
    editBox.setText(text)

    """ Now there'd be a function that finds misspelled words """

    # Highlight misspelled words
    misspelledWord = 'tish'
    cursor = editBox.textCursor()
    format_ = QtGui.QTextCharFormat()
    format_.setBackground(QtGui.QBrush(QtGui.QColor("pink")))
    pattern = "\\b" + misspelledWord + "\\b"
    regex = …
Run Code Online (Sandbox Code Playgroud)

python formatting pyqt qtextedit python-2.7

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

QTabWidget-选项卡图标不在中心

我有一个带有六个选项卡的QTabWidget,所有选项卡都有一个图标-但图标不在选项卡的中心:

标签图标

到目前为止,我所做的是:

tabWidget->setStyleSheet("QTabBar::tab {width: 40px; height: 40px;}"
                        "QTabBar::tab:selected {background: lightblue;}");
tabWidget->setIconSize(QSize(40, 40));
tabWidget->addTab("widget", QIcon("iconPath"), ""); //<--for all six tabs
Run Code Online (Sandbox Code Playgroud)

和:

tabWidget->setTabIcon(index, QIcon("iconPath"));
Run Code Online (Sandbox Code Playgroud)

有什么想法为什么会发生这种情况,以及如何解决?

icons qt tabs widget tabwidget

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

Qt WebEngine 中 QWebInspector 的替代品是什么?

代码片段的示例在这里:

from PySide2 import QtCore, QtGui, QtWidgets, QtWebChannel
from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings

class AppWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.view = WebView(self)
        self.setCentralWidget(self.view)
        self.view.settings().setAttribute(QWebEngineSettings.JavascriptEnabled, True)
        self.view.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True);
        self.page = self.view.page()
        self.page.setDevToolsPage(self.page)

        #self.inspector = QWebInspector(self) # Crash is here!!!
        #self.inspector.setPage(self.page)
        #self.inspector.hide()
Run Code Online (Sandbox Code Playgroud)

上面的三行在以前的版本中运行良好。

QWebInspectorPySide2的替代方案是什么?

python web-inspector python-3.x pyside2 qwebengineview

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

如何将文件放入 PyQt5 中的 QTableWidget 中

我正在尝试创建一个表格小部件,我可以在其中删除音频文件并用文件名、文件大小等填充表格。我的代码如下。我还没有实现用于读取 DragEnterEvent 上的元数据的部分,因为该表甚至不接受任何类型的删除。

from PyQt5 import QtCore, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(982, 615)
        MainWindow.setAcceptDrops(True)
        MainWindow.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(360, 0, 271, 181))
        self.label.setText("")
        self.label.setObjectName("label")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(10, 200, 701, 331))
        self.tableWidget.setAcceptDrops(True)
        self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.tableWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
        self.tableWidget.setDefaultDropAction(QtCore.Qt.CopyAction)
        self.tableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.tableWidget.setShowGrid(False)
        self.tableWidget.setSortingEnabled(True)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.horizontalHeader().setVisible(True)
        self.tableWidget.verticalHeader().setVisible(False)
        self.tableWidget.setRowCount(6)
        self.tableWidget.setColumnCount(6)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 982, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.titleUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def dragEnterEvent(self, event):
        event.accept()

    def dropEvent(self, event):
        event.accept()

    def titleUi(self, …
Run Code Online (Sandbox Code Playgroud)

python drag-and-drop file qtablewidget pyqt5

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

PyQt5中检测外部键盘事件

如何在 PyQT5 中实现关键侦听器?即使应用程序处于后台,我也想检测按键。

from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys


class Window(QWidget):
    
    ...
       

    def keyPressEvent(self, e): # doesnt work when app is in background
        if e.key() == Qt.Key_F3:
            print(1)
        elif e.key() == Qt.Key_F4:
            print(0)

   ...

        
App = QApplication(sys.argv)
App.setStyle('Fusion')
window = Window()
sys.exit(App.exec())


Run Code Online (Sandbox Code Playgroud)

python keyboard-events keylogger pyqt5

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

Qt 的米勒专栏?

有没有一种简单的方法可以在 Qt4 中拥有类似 iTunes 的界面(使用Miller Columns),还是我必须自己创建一个 MillerColumnWidget ?

python user-interface qt4 pyqt

0
推荐指数
1
解决办法
258
查看次数