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 can I access an element on a form from a function?
Forum Updated to NodeBB v4.3 + New Features

How can I access an element on a form from a function?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 601 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.
  • RezistenceR Offline
    RezistenceR Offline
    Rezistence
    wrote on last edited by
    #1

    Hello everyone, i start to learn QT and met with a problem to access an element on a form.
    For example i need to get access to listWidget (QListWidget)

    void Output(QSet<int> numberList) {
        QListWidget *listWidget = new QListWidget();
    
        QSetIterator<int> iterator(numberList);
        while(iterator.hasNext()) {
            listWidget -> addItem(QString::number(iterator.next()));
        }
    
        delete listWidget;
    }
    

    If possible then write some accessor methods, for example easy and the one that is best to use

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Within the class code of the form/widget made by Designer you can generally access the individual widget through the ui pointer thus, ui->listWidget->addItem(...). In general, the widgets that make up a form are private members of the class and would not be exposed directly outside.

      Your example code could be a member function:

      void FormClass::Output(const QSet<int>& numberList) {
          QSetIterator<int> iterator(numberList);
          while(iterator.hasNext()) {
              ui->listWidget -> addItem(QString::number(iterator.next()));
          }
      }
      
      RezistenceR 1 Reply Last reply
      0
      • C ChrisW67

        Within the class code of the form/widget made by Designer you can generally access the individual widget through the ui pointer thus, ui->listWidget->addItem(...). In general, the widgets that make up a form are private members of the class and would not be exposed directly outside.

        Your example code could be a member function:

        void FormClass::Output(const QSet<int>& numberList) {
            QSetIterator<int> iterator(numberList);
            while(iterator.hasNext()) {
                ui->listWidget -> addItem(QString::number(iterator.next()));
            }
        }
        
        RezistenceR Offline
        RezistenceR Offline
        Rezistence
        wrote on last edited by
        #3

        @ChrisW67 Access as I understand it happens through "::"? And the function must be declared in a private form block?

        JonBJ 1 Reply Last reply
        0
        • RezistenceR Rezistence

          @ChrisW67 Access as I understand it happens through "::"? And the function must be declared in a private form block?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Rezistence
          You really need to explain much better what you are really asking/wanting. For example, your sample code makes no sense: your function creates a QListWidget locally, does not add it to any UI, puts some items in it, and then destroys it. It "achieves nothing". Consequently it's hard to understand what you really have in mind. Furthermore, Output() is a free function, and as such would have no access to the UI anyway.

          @ChrisW67's code is rather different. It's a FormClass member method. It assumes you have created a QListWidget in Designer. It does not create or destroy that widget. It accesses it through the ui pointer created when you process and compile a Designer FormClass.ui file. It would only work as-is if defined in the FormClass.cpp file.

          RezistenceR 1 Reply Last reply
          0
          • JonBJ JonB

            @Rezistence
            You really need to explain much better what you are really asking/wanting. For example, your sample code makes no sense: your function creates a QListWidget locally, does not add it to any UI, puts some items in it, and then destroys it. It "achieves nothing". Consequently it's hard to understand what you really have in mind. Furthermore, Output() is a free function, and as such would have no access to the UI anyway.

            @ChrisW67's code is rather different. It's a FormClass member method. It assumes you have created a QListWidget in Designer. It does not create or destroy that widget. It accesses it through the ui pointer created when you process and compile a Designer FormClass.ui file. It would only work as-is if defined in the FormClass.cpp file.

            RezistenceR Offline
            RezistenceR Offline
            Rezistence
            wrote on last edited by Rezistence
            #5

            @JonB
            I just need to display the items with set which is passed as a parameter to a ListWidget which is already set via the designer on the form. ChrisW67 answer help me, my function my function just didn't see the ui pointer.

            JonBJ 1 Reply Last reply
            0
            • RezistenceR Rezistence

              @JonB
              I just need to display the items with set which is passed as a parameter to a ListWidget which is already set via the designer on the form. ChrisW67 answer help me, my function my function just didn't see the ui pointer.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Rezistence
              Only a a class member function where the ui is defined can see it, like @ChrisW67's FormClass::Output(), not your original Output().

              RezistenceR 1 Reply Last reply
              0
              • JonBJ JonB

                @Rezistence
                Only a a class member function where the ui is defined can see it, like @ChrisW67's FormClass::Output(), not your original Output().

                RezistenceR Offline
                RezistenceR Offline
                Rezistence
                wrote on last edited by
                #7

                @JonB Yes I got it, 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