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: controls widget on the form from the class

How to: controls widget on the form from the class

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 2.6k 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.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    hello gurus,

    i am learning something new with QT. I added a combo box on the Form using the Qt designer
    lets call it combobox1.

    In the main, I have a new class called MainWindow. In this class I need to add text to the combobox. How can I do it?
    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    --- cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWidget>
    #include <QtGui>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    ui->comboBox->addItem("1");
    ui->setupUi(this);
    

    }
    @

    Program breaks unexpectedly

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      welcome to devnet

      I have updated your post with code tags. That way it is easier to read. Please have a look to the "forum help. ":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01

      Concerning your issue you need to give more details on problem. Do you receive an error message? Please post content.

      Also some details on your environment are required (OS, compiler, IDE, etc.).

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        The problem in the code is the order of lines 12 and 13 (in the MainWindow) constructor. The ui->combobox object does not exist yet before your call to ui->setupUi(). That means that your line before the setupUi call will trigger a segfault.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raaghuu
          wrote on last edited by
          #4

          @mods - shouldn't this be moved to General and Desktop? This is not a C++ problem...
          @sherif - what Andre said should be the solution

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [quote author="raaghuu" date="1343578419"]@mods - shouldn't this be moved to General and Desktop? This is not a C++ problem...
            @sherif - what Andre said should be the solution[/quote]
            Good point on the moving... Will move to General and Desktop.

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              Why not use the QtDesigner to add the text? You can setup the combo box items there.
              have fun coding!

              Greetz, Jeroen

              1 Reply Last reply
              0
              • N Offline
                N Offline
                Neutron Stein
                wrote on last edited by
                #7

                QtDesigner is easy to use for simple manipulations. Once it becomes complex you have to use code

                Never Seen !

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  I think there is a healthy mixture to be found. Use designer where you can, and hand-coded UI's where that is needed. The one is not better than the other, they just have different pros and cons. Good thing is: you're free to mix the two styles as needed in your project.

                  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