-1

Having a QTableWidgets with a list of tables and two additional classes Combocells and Groupcells. They are shown with QTabWidget. I want to click and highlight a row or cell in the table then the two classes Combocells and Groupcells refreshs and loads into QTabWidget according to row of table.

The structure of files.

  • Main.py
  • Tablecells.py
  • Combocells.py
  • Groupcells.py

Visualisation

enter image description here

enter image description here

enter image description here

Update:

I have updated the code, signal and slot catch row number and send it. Despite by clicking on cells Data and Value tabs add to tabwidgets and removes, when row 0 is selected. what I do not get is when row 1 is selected, well Data and Value tabs add to widget, make some changes in combobox and Qlineedit inside Data and Value to see, when row 2 is clicked, would Data and Value really add to tabwidget from beginning. I notice it does not update. I have tried with addTab and insertTab with self.update(), still does not really process in a way I want to do.

Could anyone know how to fix this issue? Review this piece of script which has problem.

class Tabwidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super( Tabwidget, self).__init__() 
-----------------------
   @QtCore.pyqtSlot(int) 
   def rowselected_tables(self, row):
        print('Row {} is selected.'.format(row))
        if row > 0:
            self.update()
            #self.Tab.addTab( self.Combo, 'Data')
            #self.Tab.addTab( self.Group, 'Values')
            self.Tab.insertTab( 1, self.Combo, 'Data')
            self.Tab.insertTab( 2, self.Group, 'Values')

I want to load data and values tabs accordingly to number of rows. for example when user clicks and highlights a certain row inside the table the classes are loaded and any change in Data and Values for that row should remain, when user highlight another row then new Data and Value appears, but in case of return to previous highlighted and clicked row or cell any previous changes in Data and Values would appear again.

I want to print something like.

Row x is highligted: Data : ( L6, 0,10) and Value: (10)

enter image description here

UPDATED:

Main.py

import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from Combocells import Combocells 
from Groupcells import Groupcells 
from Tablecells import Tablecells 

class Tabwidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super( Tabwidget, self).__init__() 
        self.sizeHint()
        Tab = QtWidgets.QTabWidget()

        self.Table = Tablecells()
        self.Combo = Combocells()
        self.Group = Groupcells()

        Tab.addTab( self.Table, 'Tables')
        #Tab.addTab( self.Combo, 'Data')
        #Tab.addTab( self.Group, 'Values')

        self.Table.rownumber.connect(self.rowselected_tables)


        self.Tab.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(self.Tab)
        self.setLayout(vboxlayout)

    @QtCore.pyqtSlot(int) 
    def rowselected_tables(self, row):
        print('Row {} is selected.'.format(row))
        if row > 0:
            self.update()
            #self.Tab.addTab( self.Combo, 'Data')
            #self.Tab.addTab( self.Group, 'Values')
            self.Tab.insertTab( 1, self.Combo, 'Data')
            self.Tab.insertTab( 2, self.Group, 'Values')

        else:
            for n in [2,1]:
                self.Tab.removeTab( n )

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Tabwidget()
    w.show()
    sys.exit(app.exec_())

UPDATED:

Tablecells.py

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Tablecells(QtWidgets.QWidget):
    data = [("1", "Login",       "1", "test_login_s"), 
        ("2", "Logout",      "1", "test_logout_s"), 
        ("3", "User > Edit", "1", "test_user_edit_s")]
    rownumber = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(Tablecells, self).__init__(parent)
        self.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))
        self.tableWidget = QtWidgets.QTableWidget(0, 4)
        self.tableWidget.setHorizontalHeaderLabels(["Id", "Test name", "Owner", "Type"])
        self.tableWidget.cellClicked.connect(self.cellClick)
        self.setTableWidget()
        self.getrow()

        self.lay = QtWidgets.QHBoxLayout(self)
        self.lay.addWidget(self.tableWidget)

    def setTableWidget(self):    
        for r, (_id, _name, _owner, _type) in enumerate(self.data):
            it_id     = QtWidgets.QTableWidgetItem(_id)
            it_name   = QtWidgets.QTableWidgetItem(_name)
            it_owner  = QtWidgets.QTableWidgetItem(_owner)
            it_type = QtWidgets.QTableWidgetItem(_type)

            self.tableWidget.insertRow(self.tableWidget.rowCount())
            for c, item in enumerate((it_id, it_name, it_owner, it_type)):
                self.tableWidget.setItem(r, c, item)

    def cellClick(self, row, column):
        self.row = row
        self.column = column     
        print(self.row , self.column)
        self.rownumber.emit(self.row)

    def getrow(self):
        it = self.tableWidget.currentRow()
        print(it)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Tablecells()
    ex.show()
    sys.exit(app.exec_())

Combocells.py

from PyQt5 import QtCore, QtGui, QtWidgets
class Combocells(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Combocells, self).__init__(parent)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))

        self.combo_exclass = QtWidgets.QComboBox()
        self.combo_exclass.addItems([" Type 1       "," Type 2       "," Type 3       "," Type 4       "," Type 5       "])

        self.combo_lclass = QtWidgets.QComboBox()
        self.combo_lclass.addItems(["L2","L4","L6","L8"])

        self.combo_vct = QtWidgets.QComboBox()

        self.combo_vct.addItems(["0.10","0.20","0.30","0.40",
                                        "0.50","0.60","0.70"])

        self.combo_in = QtWidgets.QComboBox()
        self.combo_in.addItems(["Class1","Class2","Class3"])        

        self.tbox = QtWidgets.QHBoxLayout()
        self.exclass = QtWidgets.QLabel("Class1: ")
        self.tbox.addWidget(self.exclass)
        self.tbox.addWidget(self.combo_exclass)


        self.mtbox = QtWidgets.QHBoxLayout()
        self.lclass = QtWidgets.QLabel("Class2: ")

        self.mtbox.addWidget(self.lclass)
        self.mtbox.addWidget(self.combo_lclass)


        self.mbbox = QtWidgets.QHBoxLayout()
        self.vct = QtWidgets.QLabel("Class3: ")
        self.mbbox.addWidget(self.vct)
        self.mbbox.addWidget(self.combo_vct)

        self.bbox = QtWidgets.QHBoxLayout()
        self.inl = QtWidgets.QLabel("Class4: ")
        self.bbox.addWidget(self.inl)
        self.bbox.addWidget(self.combo_in)

        self.grid = QtWidgets.QGridLayout()
        self.grid.addLayout(self.tbox, 0, 0, 1, 2)
        self.grid.addLayout(self.mtbox, 1, 0)
        self.grid.addLayout(self.mbbox, 2, 0)
        self.grid.addLayout(self.bbox, 3, 0)

        Environment_Group = QtWidgets.QGroupBox()
        Environment_Group.setTitle("&Group2")
        Environment_Group.setLayout(self.grid)

        vlay = QtWidgets.QVBoxLayout(self)
        vlay.addWidget(Environment_Group)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Combocells()
    w.show()
    sys.exit(app.exec_())

Groupcells.py

from PyQt5 import QtCore, QtGui, QtWidgets
class Groupcells(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Groupcells, self).__init__(parent)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))      

        self.c_lay = QtWidgets.QHBoxLayout()
        fctd = "One\n\nTwo\n\nThree"
        con_strength = QtWidgets.QLabel(fctd)
        self.value = QtWidgets.QLineEdit('Test')
        self.c_lay.addWidget(con_strength)
        self.c_lay.addWidget(self.value, alignment=QtCore.Qt.AlignRight)


        self.combo = QtWidgets.QComboBox()
        self.combo.addItems(["10","12","14","16"])

        self.hbox = QtWidgets.QHBoxLayout()
        self.con = QtWidgets.QLabel("Number: ")
        self.hbox.addWidget(self.con)
        self.hbox.addWidget(self.combo)

        self.vlay = QtWidgets.QVBoxLayout()
        self.vlay.addLayout(self.hbox)
        self.vlay.addLayout(self.c_lay)
        self.vlay.addStretch()

        Concrete_Group = QtWidgets.QGroupBox()
        Concrete_Group.setTitle("&GroupA")
        Concrete_Group.setLayout(self.vlay)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(Concrete_Group)

        self.comth = ["10","12","14","16"]
        self.combo.activated.connect(self.setdatastrength)

    @QtCore.pyqtSlot(int)
    def setdatastrength(self, index):
        value = self.comth[index]
        self.display_data(value)


    def display_data(self, value):
        try:
            f = value
            f_value = "{}"
            self.value.setText(f_value.format(f))

        except ValueError:
            print("Error")

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Groupcells()
    w.show()
    sys.exit(app.exec_()) 

I really do not know how to achieve that. It might be some special scripts in PyQT5 to do the task. I appreciate any help. Thanks.

Pavel.D
  • 561
  • 1
  • 15
  • 41
  • Sorry, I read it several times and didn’t understand what you want to update? For example: if you click on the cells of a row with index 1, what do you want to see inside the `Value` tab? – S. Nick Sep 04 '19 at 17:36
  • @S.Nick. what I want to see is, when cells of row with index 1 clicks, Data and Value tabs remember the values for example combo value for class 3 is changed to 0.30, after selecting cells of row 2, values inside Combobox and others in Data and Value tabs refresh and reset for example combo value to 0.10. Hope I have explained well. – Pavel.D Sep 04 '19 at 19:33

1 Answers1

1

If I understand your question correctly you want the "Group" and "Combo" tabs to remember the values you set for a specific row and restore these values when you select the row again after having selected a different row. In that case you could do something like below (GroupCells.py and ComboCells.py are the same as before)

In Main.py

class Tabwidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super( Tabwidget, self).__init__()
        self.sizeHint()
        self.Tab = QtWidgets.QTabWidget()

        self.data = [("1", "Login", "1", "test_login_s"),
                ("2", "Logout", "1", "test_logout_s"),
                ("3", "User > Edit", "1", "test_user_edit_s")]

        self.combos = []
        self.groups = []
        self.Table = Tablecells()
        for row in self.data:
            self.addRow(row)

        self.Tab.addTab( self.Table, 'Tables')
        self.Table.rownumber.connect(self.rowselected_tables)
        self.Tab.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(self.Tab)
        self.setLayout(vboxlayout)

    def addRow(self, data):
        self.Table.addRow(data)
        self.combos.append(Combocells())
        self.groups.append(Groupcells())

    @QtCore.pyqtSlot(int)
    def rowselected_tables(self, row):
        print('Row {} is selected.'.format(row))
        while self.Tab.count() > 1:
            self.Tab.removeTab(self.Tab.count()-1)
        self.Tab.addTab(self.combos[row], 'Combo')
        self.Tab.addTab(self.groups[row], 'Group')

In TableCells.py

class Tablecells(QtWidgets.QWidget):
    rownumber = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(Tablecells, self).__init__(parent)
        self.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))
        self.tableWidget = QtWidgets.QTableWidget(0, 4)
        self.tableWidget.setHorizontalHeaderLabels(["Id", "Test name", "Owner", "Type"])
        self.tableWidget.cellClicked.connect(self.cellClick)
        self.getrow()

        self.lay = QtWidgets.QHBoxLayout(self)
        self.lay.addWidget(self.tableWidget)

    def addRow(self, data):
        _id, _name, _owner, _type = data
        it_id     = QtWidgets.QTableWidgetItem(_id)
        it_name   = QtWidgets.QTableWidgetItem(_name)
        it_owner  = QtWidgets.QTableWidgetItem(_owner)
        it_type = QtWidgets.QTableWidgetItem(_type)

        self.tableWidget.insertRow(self.tableWidget.rowCount())
        for c, item in enumerate((it_id, it_name, it_owner, it_type)):
            self.tableWidget.setItem(self.tableWidget.rowCount()-1, c, item)

    def cellClick(self, row, column):
        self.row = row
        self.column = column
        print(self.row , self.column)
        self.rownumber.emit(self.row)

    def getrow(self):
        it = self.tableWidget.currentRow()
        print(it)

In this example I create a number of ComboCells and GroupCells widget equal to the number of rows in the table, and add these widgets to self.Tab if the corresponding row is selected. In order to do this I've moved the responsibility of adding the data to the table from TableCells to TabWidget and replaced TableCells.setTableWidget() with two methods: TableCells.addRow() and TabWidget.addRow(). The ComboCells and GroupCells widgets are created in TabWidget.addRow and added to self.combos and self.groupcells, respectively. These widgets are then added and removed to self.Tab in TabWidget.rowselected_tables().

Heike
  • 24,102
  • 2
  • 31
  • 45