How to make text in QLineEdit widget make it set as header in a QTableView?



  • I want to set the text of the horizontal headers in my QTableView using user typed text in several QLineEdit widgets. I'm new to model-view programming and Qt framework so I am not sure if this is the correct way to do it, but what I've got working so far is that in my MainWindow class the widget with the QLineEdits is set as the central widget and then .text() is called on all the QLineEdits. The result is passed to a list and the list of strings is passed as parameter to my model class (QAbstractTableModel). In the model I call headerData() function to display the strings in the right column headers. However since the QLineEdits are empty initially nothing is shown. Now when I try changing the text in the QLineEdit it does not update the headers and thus makes the headers stay empty. Is there a way I can update the text in the QLineEdit so that it updates to my model too, or is my approach completely wrong? Appreciate any help. If code examples are needed I can post them, but they're in Python.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    This post might be what you need and it's already in python.

    Hope it helps



  • connect(firstColumnLineEdit,&QLineEdit::editingFinished,[=]()->void{model->setHeaderData(0,Qt::Horizontal,firstColumnLineEdit->text());
    connect(secondColumnLineEdit,&QLineEdit::editingFinished,[=]()->void{model->setHeaderData(1,Qt::Horizontal,firstColumnLineEdit->text());
    // and so on
    


  • @VRonin Thanks for the replies, however I am not really familiar with C++ syntax even though I really tried translating your comment to python . Could you maybe briefly explain in words what is happening here concerning signals and slots and also what the reason is that setHeaderData function is used instead of headerData function in this case?



  • Oh, sorry, I missed the python part.
    you basically have to connect the editingFinished signal of the line edit to a slot that uses what's in the line edit to populate the header.

    @ET97 said in How to make text in QLineEdit widget make it set as header in a QTableView?:

    what the reason is that setHeaderData function is used instead of headerData function in this case?

    headerData retrieves what's already in the header, setHeaderData writes what should be in the header



  • @VRonin Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.