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 706 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.
  • R Offline
    R Offline
    Rezistence
    wrote on 11 Aug 2022, 21:05 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 11 Aug 2022, 21:47 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()));
          }
      }
      
      R 1 Reply Last reply 12 Aug 2022, 08:34
      0
      • C ChrisW67
        11 Aug 2022, 21:47

        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()));
            }
        }
        
        R Offline
        R Offline
        Rezistence
        wrote on 12 Aug 2022, 08:34 last edited by
        #3

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

        J 1 Reply Last reply 12 Aug 2022, 08:41
        0
        • R Rezistence
          12 Aug 2022, 08:34

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

          J Offline
          J Offline
          JonB
          wrote on 12 Aug 2022, 08:41 last edited by JonB 8 Dec 2022, 08:45
          #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.

          R 1 Reply Last reply 12 Aug 2022, 09:28
          0
          • J JonB
            12 Aug 2022, 08:41

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

            R Offline
            R Offline
            Rezistence
            wrote on 12 Aug 2022, 09:28 last edited by Rezistence 8 Dec 2022, 09:32
            #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.

            J 1 Reply Last reply 12 Aug 2022, 09:54
            0
            • R Rezistence
              12 Aug 2022, 09:28

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

              J Offline
              J Offline
              JonB
              wrote on 12 Aug 2022, 09:54 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().

              R 1 Reply Last reply 12 Aug 2022, 10:05
              0
              • J JonB
                12 Aug 2022, 09:54

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

                R Offline
                R Offline
                Rezistence
                wrote on 12 Aug 2022, 10:05 last edited by
                #7

                @JonB Yes I got it, thanks

                1 Reply Last reply
                0

                5/7

                12 Aug 2022, 09:28

                • Login

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