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. Instantiating gui object issue

Instantiating gui object issue

Scheduled Pinned Locked Moved General and Desktop
37 Posts 4 Posters 11.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.
  • D Offline
    D Offline
    Dn588
    wrote on last edited by
    #26

    i'm currently busy with a university course that uses QT 4 which is why I am using it. I just commented out the slots but still not getting a main window... I created a new project and added the order and OrderForm files to it and it ran with no issues... So i'm guessing there's also something wrong with my main window's constructor?

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #27

      Oh yeah I thought it was supposed to be like that, that's why I said it is a little weird your app starts with a menu and not the main window, so that is a bug too?

      comment out this line
      @
      orderMenu->exec();
      @
      in your ordergui::createMenu() function.
      you should sometimes read the Qt doc, it tells you that QMenu::exec() "Executes this menu synchronously." which means it will block the thread until you close the menu (click an item) and only after that display the window. if you don't call "exec" it will just show the main window with your menu and your text widget (like it should?).

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dn588
        wrote on last edited by
        #28

        Thanks a lot for all your effort Xander84. I read the QMenu reference page but misunderstood it. I thought exec() has to be called to activate the menu...

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dn588
          wrote on last edited by
          #29

          I now have to add all orders to a list that gets displayed when view_orders_clicked is triggered.

          What i've tried doing is create a function in OrderForm

          QString addToList() { return m_order.toString(); }

          then in my ordergui class I created a new varieable

          QStringList OrderList;

          So now the add_order_clicked slot looks like this:
          @
          void ordergui::add_order_clicked() {
          OrderForm* obj;
          setCentralWidget(obj);
          OrderList.append(obj->addToList());
          view_orders->setEnabled(true);;
          }
          @
          when I click on add order the program crashes every time...

          Am I going about this the right way?

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xander84
            wrote on last edited by
            #30

            you have not allocated an object
            @
            OrderForm* obj;
            @
            is just a pointer to nothing (invalid memory), that is why your app is crashing.

            this should work:
            @
            OrderForm* obj = new OrderForm; // creates an object on the heap memory (you have to delete it yourself usually, use this)
            OrderForm obj; // creates an object on the stack memory (will be deleted if the object leaves the current scope / end of function)
            @

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dn588
              wrote on last edited by
              #31

              When I do it that way I get an error saying that the constructor is invalid as it was called with no parameters and it should be called with a QWidget*. OrderForm::OrderForm(QWidget*)...

              1 Reply Last reply
              0
              • X Offline
                X Offline
                Xander84
                wrote on last edited by
                #32

                yeah well that is because your constructor is not defined in the default "Qt way":
                @
                OrderForm(QWidget* parent = 0);
                @
                so you can create objects without any parent, because setCentralWidget will reparent the widget anyway. :)

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dn588
                  wrote on last edited by
                  #33

                  Thanks it works perfectly now:). when I try to add orders to the OrderList ad in my previous post and write it to a new instance of m_text when view_orders is triggered none of the m_order values gets read in. i'm guessing that happens because it immediately tries to call addToList in Orderform passing obj->m_Order before the ok button is clicked. How could I go about making sure that m_order gets converted to a QString and added to OrderList only after all the orderForm fields have been completed?

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    Xander84
                    wrote on last edited by
                    #34

                    I am not sure what you want to achieve but you can try using "validators":http://qt-project.org/doc/qt-4.8/qvalidator.html for the form fields? e.g.
                    "QLineEdit::setValidator(const QValidator * v)":http://qt-project.org/doc/qt-4.8/qlineedit.html#setValidator
                    and then check if the input of all fields is valid before you add it to the list or show an error next if it isn't. that is the classic approach I would say. :)

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dn588
                      wrote on last edited by
                      #35

                      My current issue isn't validating the order input. I have to create a list of orders which should then get printed to m_text when view_orders is triggered.
                      What i've done is create a QList

                      QList<Order> m_list in the orderForm class.

                      I added m_list.append() to the submit slot of OrderForm to add orders to the list every time the ok button is clicked. then I added another function to OrderForm
                      @
                      QString OrderForm::displayOrders() {
                      for (int i = 0; i < m_list.size(); ++i)
                      return m_list.at(i).toString();
                      }
                      @

                      also I changed my add_order_clicked slot to
                      @
                      void ordergui::add_order_clicked() {
                      OrderForm *obj = new OrderForm;

                      setCentralWidget(obj);
                      OrderList.append(obj->displayOrders());
                      view_orders->setEnabled(true);
                      

                      }
                      @

                      Then I changed my view_orders_clicked slot of ordergui to:

                      @
                      void ordergui::view_orders_clicked() {
                      m_text = new QTextEdit;
                      setCentralWidget(m_text);
                      for (int i = 0; i < OrderList.size(); ++i) {
                      m_text->append(OrderList.at(i));
                      }

                      }
                      @

                      whenever I add an order and then select view orders the program crashes... Any idea why that would happen?

                      1 Reply Last reply
                      0
                      • X Offline
                        X Offline
                        Xander84
                        wrote on last edited by
                        #36

                        Sounds a little chaotic to me, sorry :D
                        Your order form keeps a list of all orders and then your have a list of the string values of all orders in another class!?

                        I would suggest using a list view, why are you using a QTextEdit for a list of orders? Ok i might be a little bit easier at first, but the proper way would be to use a ListView which holds a list model with all your orders and displays them as their string representation I think? You could also use a TableView but is more complicated.

                        Maybe take a look at these articles:
                        http://qt-project.org/doc/qt-4.8/modelview.html
                        http://qt-project.org/doc/qt-4.8/model-view-programming.html

                        Also your for loop is useless:
                        @
                        QString OrderForm::displayOrders() {
                        for (int i = 0; i < m_list.size(); ++i)
                        return m_list.at(i).toString();
                        }
                        @
                        the return expression will exit the function immediately, so that is the same as
                        @
                        QString OrderForm::displayOrders() {
                        return m_list.at(0).toString();
                        }
                        @
                        if you want to concatenate the values of all strings and return those you need to use QString::append or something else to build a string and return it at the end of the function.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Dn588
                          wrote on last edited by
                          #37

                          The instructions in the exercise says to use a QTextEdig... So maybe I could change displayOrders to append all orders using QString::append?

                          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