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. [SOLVED] Add QLabel and QLineEdit from formLayout to QHash
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Add QLabel and QLineEdit from formLayout to QHash

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    republicca
    wrote on last edited by
    #1

    Hello,

    I have a FormLayout, with several rows which have one Label and one QLineEdit created like this:

    @
    for(int i=0; i<class.count(); i++)
    {
    lineEdit = new QLineEdit;
    label = new QLabel;
    label->setText(class->getName(i));
    formLayout->addRow(label, lineEdit);
    }
    @

    I want to get the added content on the lineEdit with its label to a QHashTable. I've already tried some things like using textEdited but there I can only get the content of the lineEdit. With other things like editingFinished I cannot even get that.

    I'm really stuck so any help would be appreciated!!!

    Thanks in advance!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      Hope it is QHash data structure. You need to work with signals QLineEdit.

      @connect(lineEdit,SIGNAL(returnPressed()),&w,SLOT(callme()));

      void callme(...)
      {
      QString text = lineEdit.text();

      hash.insert(label.text(),text())
      

      }@

      This should help you.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0
      • R Offline
        R Offline
        republicca
        wrote on last edited by
        #3

        Hi! Thanks!!
        I finally managed to do it but in another way, because the problem I see with your solution (I think) is that I would only get the last label. I did it like this:

        @

        for(int i=0; i<class.count(); i++)
        {
        lineEdit = new QLineEdit;
        lineEdit->setAccessibleName(class->get->Name(i));
        label = new QLabel;
        label->setText(class->getName(i));
        formLayout->addRow(label, lineEdit);

        connect(lineEdit, SIGNAL(textEdited(QString)),
        this, SLOT(addHash(QString)));
        }
        @

        And the slot would look like this:

        @
        void addHash(QString text)
        {
        QLineEdit edit = qobject_cast<QLineEdit>(sender());
        hash.insert(edit->accessibleName(), text);
        }
        @

        The key to it is the sender function which I didnt know it existed.

        Thank you!

        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