【问题标题】:obtain the number of rows of qtablewidget in pyqt获取pyqt中qtablewidget的行数
【发布时间】:2016-03-19 07:55:08
【问题描述】:

当我单击 QTableWidget(tblbimar) 中的选择按钮时,我希望给我相同的行数(QTable)。 但是当我点击“选择”按钮时,什么也没有发生!
当然我试过是否发送信号“tbl_bimar_row”,(它工作正常)

# -*- coding: utf-8 -*-

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

import os
import sys

class groupbutton(QWidget):
    tbl_bimar_row = pyqtSignal(int)
    def __init__(self,parent=None):
        super(groupbutton,self).__init__(parent)
        self.parent = parent
        btnselect = QPushButton("select")
        layout = QHBoxLayout()
        layout.setContentsMargins(0,0,0,0)
        layout.setSpacing(5)
        layout.addWidget(btnselect)
        self.setLayout(layout)
        self.connect(btnselect,SIGNAL("clicked()"),self.return_row)

    def return_row(self):
        widget_pos = QtGui.qApp.focusWidget()
        index = self.parent.indexAt(widget_pos.parentWidget().pos())
        # OR index = self.parent.indexAt(widget_pos.pos())
        if index.isValid():
            self.tbl_bimar_row.emit(index.row())


class Jarahi_add(QDialog,Ui_My_Form):
    def __init__(self,parent=None):
        super(Jarahi_add,self).__init__(parent)
        self.setupUi(self)
        self.bimar_id = None
        get_bimar()
        self.connect(self.txtbimarname,SIGNAL("textChanged(QString)"),self.get_bimar)

    def bimar_select(self,row):
        print(self.tblbimar.item(row,1).text())

    def get_bimar(self):
        data = [['1','2','3','4','5'],['6','7','8','9','10']]
        for row in data:
            inx = data.index(row)
            self.tblbimar.insertRow(inx)
            self.tblbimar.setItem(inx,0,QTableWidgetItem(row[0]))
            self.tblbimar.setItem(inx,1,QTableWidgetItem(row[1]))
            self.tblbimar.setItem(inx,2,QTableWidgetItem(row[2]))
            self.tblbimar.setItem(inx,3,QTableWidgetItem(row[3]))
            self.tblbimar.setItem(inx,4,QTableWidgetItem(row[4]))
            selbtn = groupbutton(parent=self.tblbimar)
            selbtn.tbl_bimar_row.connect(self.bimar_select)
            self.tblbimar.setCellWidget(inx,5,selbtn)
            # self.tblbimar.setItem(inx,5,QTableWidgetItem(smart_text(row[0])))
            self.tblbimar.resizeColumnsToContents()
            self.tblbimar.resizeRowsToContents()

谢谢

【问题讨论】:

  • 当我使用 QMainWindow 它工作但如果我使用 QDialog 不起作用!?

标签: pyqt qtablewidget


【解决方案1】:

我将上面的代码改成如下,它工作了:

class groupbutton(QWidget):
    tbl_bimar_row = pyqtSignal(int)
    def __init__(self,parent=None,app=None):
        super(tblbutton,self).__init__(parent)

        self.parent = parent
        self.app = app  
        ...
        ...
    def return_row(self):
        widget_pos = self.app.focusWidget()
        index = self.parent.indexAt(widget_pos.parentWidget().pos())
        self.tbl_bimar_row.emit(index.row())
class Jarahi_add(QDialog,Ui_My_Form):
    ...  
    ... 
    def get_bimar(self):

        if data:
            for row in data:
                ...  
                ...  
                selbtn = tblbutton(parent=self.tblbimar,app=self)
                selbtn.tbl_bimar_row.connect(self.bimar_select)
                self.tblbimar.setCellWidget(inx,5,selbtn)

【讨论】:

    猜你喜欢
    • 2017-03-21
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    相关资源
    最近更新 更多