user10567350
user10567350

Reputation:

Change QTableWidget Header Labels using a QLineEdit

So i've made a Table in QtDesigner and i want to use a QLineEdit.text() to name its headers.

The QLineEdit will be respresented with square brackets []. the QPushButton Will be represented with curly brackets{}.

Column name: [placeholdertext ] {Name}

I am using a QspinBox for the indexes.

Now what i want, is to give the user the possibility of naming all columns simply by typing [First_name, Last_name, Id_Number, ...]but i dont know how to name the headers neither how to use the split thing

How can i make this happen ?

Update :

enter image description here

def NameHeaders(self):
    colpos = self.ColumnSpinBox.value()
    colname = self.nameColumnLineEdit.text()
    model = QtGui.QStandardItemModel()
    model.setVerticalHeaderLabels(colname, split(","))
    self.TableWidget.setModel(model)

this is the function i've created linked to

"Name column/Row" Button

(for now it focuses only on naming the columns not the rows),

so what i want is to name the columns just by typing in the column QlineEdit something like : First_name, Last_name, Id_number, ...

and i want the code to detect the text between commas and assign each text to the the value of the QSpinBox

Example :

QSpinBoxValue: 2 || Column name : First_name, Last_name, id_number


On_Click 'Name Column/Row' Button: 


assign First_name to Header with index 0


assign Last_name to header with index 1


assign Id_Number to header with index 2

is my example clear?

Upvotes: 2

Views: 4906

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

As you want to update the QSpinBox with the number of words between commas the first thing is to use the textChanged signal of QLineEdit so that it notifies each time that text is changed, separate the words, count them and update the QSpinBox. To set the text in the headers you must use setHorizontalHeaderLabels(), but before that you must change the number of columns if necessary.

from PyQt5 import QtCore, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.table_widget = QtWidgets.QTableWidget(4, 6)
        self.spinbox = QtWidgets.QSpinBox()
        self.le = QtWidgets.QLineEdit()
        self.le.textChanged.connect(self.on_textChanged)
        button = QtWidgets.QPushButton("Change")
        button.clicked.connect(self.on_clicked)

        lay = QtWidgets.QVBoxLayout(self)
        hlay = QtWidgets.QHBoxLayout()
        hlay.addWidget(self.spinbox)
        hlay.addWidget(self.le)
        hlay.addWidget(button)
        lay.addWidget(self.table_widget)
        lay.addLayout(hlay)

    @QtCore.pyqtSlot(str)
    def on_textChanged(self, text):
        words = text.split(",")
        n_words = len(words)
        self.spinbox.setValue(n_words)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        words = self.le.text().split(",")
        n_words = len(words)
        if n_words > self.table_widget.columnCount():
            self.table_widget.setColumnCount(n_words)
        self.table_widget.setHorizontalHeaderLabels(words)


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

Upvotes: 3

Related Questions