Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make text in QLineEdit widget make it set as header in a QTableView?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ET97
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3
        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
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        E 1 Reply Last reply
        0
        • VRoninV VRonin
          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
          
          E Offline
          E Offline
          ET97
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            E 1 Reply Last reply
            0
            • VRoninV VRonin

              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

              E Offline
              E Offline
              ET97
              wrote on last edited by
              #6

              @VRonin Thanks!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved