Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Using function to do stuff in UI?
QtWS25 Last Chance

Using function to do stuff in UI?

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 3 Posters 1.5k 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.
  • P Offline
    P Offline
    Parre
    wrote on last edited by
    #1

    Hi!

    I've been trying to for the past few days to create a small GUI-application in Qt and C++ but i'm having trouble calling a function to change stuff in the ui.

    I'm having a really hard time wrapping my head around what, how and why the function call needs to be done .

    Here's exactly what i want to do:
    I have a Combobox in the ui that i want to add items to once at the start of the application and then every time a "Reload" button is pressed.

    I thought a function to do this would be best and then just call the function whenever i need to update the Combobox. Qt appearantly doesn't think the same.

    I understand that i need to pass the data that i want in the combobox to the MainWindow somehow but i'm completely lost. I also don't know what i should type in google to find anything about it.

    Please explain how this is done and also why.

    //
    Anders

    1 Reply Last reply
    0
    • B Offline
      B Offline
      belab
      wrote on last edited by
      #2

      Hi Parre,
      I would do it straight forward like this:
      @
      void init() {
      updateComboBox(); // once at start time
      }
      void reload() {
      updateComboBox(); // every time "Reload" was pressed
      }
      void updateComboBox() {
      ui->comboBox->clear(); // clear all contents
      // fill with new content
      for( int i = 0; i < myComboBoxData.size(); ++i ) {
      ui->comboBox->addItem(myComboBoxData[i].displayName);
      }
      }
      @

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Parre
        wrote on last edited by
        #3

        That's what i've done but i get the error message:
        error: 'ui' was not declared in this scope

        I can 't see why it shouldn't work :S

        [quote author="belab" date="1394391703"]Hi Parre,
        I would do it straight forward like this:
        @
        void init() {
        updateComboBox(); // once at start time
        }
        void reload() {
        updateComboBox(); // every time "Reload" was pressed
        }
        void updateComboBox() {
        ui->comboBox->clear(); // clear all contents
        // fill with new content
        for( int i = 0; i < myComboBoxData.size(); ++i ) {
        ui->comboBox->addItem(myComboBoxData[i].displayName);
        }
        }
        @[/quote]

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi Parre,

          First of all, I recommend going through this tutorial: http://doc-snapshot.qt-project.org/qt5-stable/gettingstartedqt.html

          Anyway, regarding your case,it depends on how your MainWindow is set up. You can find out by studying mainwindow.h.

          belab's solution works if your MainWindow contains a ui pointer. However, your MainWindow probably inherits the object instead of storing its pointer. In this case, just remove "ui->". The names of your combo box variables are set in Qt Designer, under "objectName".

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          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