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 14.8k 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
    #3

    Yes i'm sure i'm using the correct name. which base class are you talking about?
    this code is copied from another project example. What does it mean when in the constructor QWidget is initialized to parent? What is the parent of QWidget?
    Thanks again
    Deon

    code for orderForm implimentation file:
    @
    #include "orderform.h"
    #include "order.h"
    #include <QFormLayout>

    //constructor
    OrderForm::OrderForm(QWidget *parent):QWidget(parent){
    setUpGui();
    }

    //sets up the GUI
    void OrderForm::setUpGui(){
    m_nameEdit = new QLineEdit();
    m_dateEdit = new QDateEdit();
    m_quantitySpin = new QSpinBox();
    m_quantitySpin->setRange(1,1000);
    m_priceSpin = new QDoubleSpinBox();
    m_priceSpin->setRange(1.0,1000.00);
    m_totalEdit = new QLineEdit();
    m_totalEdit->setReadOnly(true);
    m_submitButton = new QPushButton("OK");
    m_cancelButton = new QPushButton("Cancel");

    QGridLayout* gridLayout = new QGridLayout();
    gridLayout->addWidget(m_submitButton,0,0);
    gridLayout->addWidget(m_cancelButton,0,1);
    
    QFormLayout *formLayout = new QFormLayout();
    formLayout->addRow(tr("&Name"), m_nameEdit);
    formLayout->addRow(tr("&Date Added"), m_dateEdit);
    formLayout->addRow(tr("&Quantity"), m_quantitySpin);
    formLayout->addRow(tr("&Unit Price"), m_priceSpin);
    formLayout->addRow(tr("&Total Price"), m_totalEdit);
    formLayout->addRow(gridLayout);
    
    connect(m_submitButton, SIGNAL(clicked()), this, SLOT(submit()));
    connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(m_quantitySpin, SIGNAL(editingFinished()), this, SLOT(calculateCost()));
    connect(m_priceSpin, SIGNAL(editingFinished()), this, SLOT(calculateCost()));
    
    this->setLayout(formLayout);
    this->setWindowTitle("Manual Order Form");
    

    }

    // sets an order
    void OrderForm::setOrder(Order prod){
    m_Order = prod;
    viewOrder();
    }

    //create a new order
    void OrderForm::submit(){
    m_Order.setName(m_nameEdit->text());
    m_Order.setDateAdded(m_dateEdit->date());
    m_Order.setQuantity(m_quantitySpin->value());
    m_Order.setPrice(m_priceSpin->value());
    }

    //view an order
    void OrderForm::viewOrder(){
    m_nameEdit->setText(m_Order.name());
    m_dateEdit->setDate(m_Order.dateAdded());
    m_quantitySpin->setValue(m_Order.quantity());
    m_priceSpin->setValue(m_Order.price());
    calculateCost();
    }
    //calculate the cost of an order
    void OrderForm::calculateCost(){
    double total = double(m_quantitySpin->value()* m_priceSpin->value());
    m_totalEdit->setText(QString::number(total, 'f', 2));
    }

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Your class is not called orderForm but OrderForm, that's why it fails. Casing is important.

      Have look "here":http://qt-project.org/doc/qt-5/qwidget.html#QWidget to get an explanation about the parent of QWidget

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Thanks SGaist stupid error on my side... What do you mean by the base constructor isn't called in the constructor? I get the program to run but there's no gui output

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Since both are QWidget:

          Clean way:
          @
          OrderForm::OrderForm(QWidget *parent):
          QWidget(parent){

          }@

          Only if you don't expect to parent your widget in the constructor:
          @
          ordergui::ordergui() {
          }@

          Edit: corrected the wording

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

            Thanks again... So I obviously didn't understand the explanation on the previous link you posted. What I understood from there was that if QWidget* parent = 0; it will be a window otherwise it would be a widget on a window?

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

              When I do that I get an error saying QWidget is not a direct base type of ordergui...

              Also why was that not done in this example?

              http://qt-project.org/doc/qt-4.8/mainwindows-application.html

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #9

                I've updated the wording of my last post (was a bit tired)

                Since you have that error, it means that ordergui doesn't inherit from QWidget. It should inherit from QMainWindow.

                Since you don't see anything do you call show somewhere in you main.cpp ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                  Yes I call show from main.cpp but for some reason I don't get any output, any idea what i'm doing wrong?

                  main.cpp:

                  @
                  #include <QtGui>
                  #include <QApplication>
                  #include "ordergui.h"

                  int main(int argc, char *argv[]) {
                  QApplication app(argc, argv);
                  ordergui order;

                  order.show();
                  return app.exec();
                  

                  }
                  @

                  1 Reply Last reply
                  0
                  • mranger90M Offline
                    mranger90M Offline
                    mranger90
                    wrote on last edited by
                    #11

                    could it be becuase you orderForm is created on the stack before being sent to setCentralWidget ? It then goes out of scope and is not available.
                    You should allocate it off the heap with "new".

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

                      I just tried that but still no luck...

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

                        Just wondering, how can you even call setCentralWidget without a pointer to QWidget? :)
                        @
                        void QMainWindow::setCentralWidget(QWidget * widget)
                        @
                        you should get a compile error here:
                        @
                        orderForm obj;
                        setCentralWidget(obj);
                        @

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

                          Hi Xander. Sorry yes I changed it to OrderForm* obj but for some reason there's no output. Also when I try to close the running program I have to force quit

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

                            [quote author="Dn588" date="1397930234"]Hi Xander. Sorry yes I changed it to OrderForm* obj but for some reason there's no output. Also when I try to close the running program I have to force quit[/quote]
                            Then you have most like created another (invisible) top level windows, if you have to "kill" the app.
                            you can try setting the parent of your central widget, I though the setCentralWidget would reparent it, but maybe not:
                            @
                            setCentralWidget(new orderForm(this));
                            @
                            maybe that works.

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

                              No still no visible output and I still have to force quit...

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

                                Is it possible that you can upload your whole project code somewhere? I don't know if you can publish your code or how large your codebase is beside what you already posted, but an logic error like this is hard to debug over the forums.. :/

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

                                  My Code:

                                  @
                                  order.h

                                  #ifndef ORDER_H
                                  #define ORDER_H

                                  #include <QString>
                                  #include <QDate>

                                  class Order {

                                  public:
                                  //getters
                                  QString name() const {return m_name;}
                                  QDate dateAdded() const { return m_added; }
                                  int quantity() const {return m_quantity; }
                                  double price() const {return m_price;}

                                  //return a string representation of an order
                                  QString toString() const;
                                  
                                  //setters
                                  void setName(const QString &name) {m_name = name;}
                                  void setDateAdded(const QDate &added) {m_added = added;}
                                  void setQuantity(int qty) {m_quantity = qty;}
                                  void setPrice(double unitPrice) {m_price = unitPrice;}
                                  

                                  private:
                                  //data members
                                  QString m_name;
                                  QDate m_added;
                                  int m_quantity;
                                  double m_price;

                                  };

                                  #endif // #ifndef ORDER_H

                                  order.cpp

                                  #include "order.h"
                                  #include <QString>

                                  //returns the string representation of an order
                                  QString Order::toString() const {
                                  return QString("%1 %2 (%3)at $ %4 total: %5").arg(m_name)
                                  .arg(m_added.toString("yyyyMMdd")).arg(m_quantity)
                                  .arg(m_price).arg(m_quantity*m_price);
                                  }

                                  OrderForm.h
                                  #ifndef ORDERFORM_H
                                  #define ORDERFORM_H

                                  #include <QtGui>
                                  #include "order.h"

                                  class OrderForm : public QWidget {
                                  Q_OBJECT

                                  public:
                                  //constructor
                                  OrderForm(QWidget* parent);
                                  //view a given order
                                  void setOrder(Order prod);

                                  private slots:
                                  //create a new order
                                  void submit();
                                  //calculate the cost of an order
                                  void calculateCost();

                                  private:
                                  //private data members
                                  QLineEdit* m_nameEdit;
                                  QDateEdit* m_dateEdit;
                                  QSpinBox* m_quantitySpin;
                                  QDoubleSpinBox* m_priceSpin;
                                  QLineEdit* m_totalEdit;
                                  QPushButton* m_submitButton;
                                  QPushButton* m_cancelButton;

                                  Order m_Order;
                                  
                                  //to view an order
                                  void viewOrder();
                                  //sets up the GUI
                                  void setUpGui();
                                  

                                  };

                                  #endif // #ifndef ORDERFORM_H

                                  OrderForm.cpp

                                  #include "orderform.h"
                                  #include "order.h"
                                  #include <QFormLayout>

                                  //constructor
                                  OrderForm::OrderForm(QWidget *parent) : QWidget(parent) {
                                  setUpGui();
                                  }

                                  //sets up the GUI
                                  void OrderForm::setUpGui(){
                                  m_nameEdit = new QLineEdit();
                                  m_dateEdit = new QDateEdit();
                                  m_quantitySpin = new QSpinBox();
                                  m_quantitySpin->setRange(1,1000);
                                  m_priceSpin = new QDoubleSpinBox();
                                  m_priceSpin->setRange(1.0,1000.00);
                                  m_totalEdit = new QLineEdit();
                                  m_totalEdit->setReadOnly(true);
                                  m_submitButton = new QPushButton("OK");
                                  m_cancelButton = new QPushButton("Cancel");

                                  QGridLayout* gridLayout = new QGridLayout();
                                  gridLayout->addWidget(m_submitButton,0,0);
                                  gridLayout->addWidget(m_cancelButton,0,1);
                                  
                                  QFormLayout *formLayout = new QFormLayout();
                                  formLayout->addRow(tr("&Name"), m_nameEdit);
                                  formLayout->addRow(tr("&Date Added"), m_dateEdit);
                                  formLayout->addRow(tr("&Quantity"), m_quantitySpin);
                                  formLayout->addRow(tr("&Unit Price"), m_priceSpin);
                                  formLayout->addRow(tr("&Total Price"), m_totalEdit);
                                  formLayout->addRow(gridLayout);
                                  
                                  connect(m_submitButton, SIGNAL(clicked()), this, SLOT(submit()));
                                  connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(close()));
                                  connect(m_quantitySpin, SIGNAL(editingFinished()), this, SLOT(calculateCost()));
                                  connect(m_priceSpin, SIGNAL(editingFinished()), this, SLOT(calculateCost()));
                                  
                                  this->setLayout(formLayout);
                                  this->setWindowTitle("Manual Order Form");
                                  

                                  }

                                  // sets an order
                                  void OrderForm::setOrder(Order prod){
                                  m_Order = prod;
                                  viewOrder();
                                  }

                                  //create a new order
                                  void OrderForm::submit(){
                                  m_Order.setName(m_nameEdit->text());
                                  m_Order.setDateAdded(m_dateEdit->date());
                                  m_Order.setQuantity(m_quantitySpin->value());
                                  m_Order.setPrice(m_priceSpin->value());
                                  }

                                  //view an order
                                  void OrderForm::viewOrder(){
                                  m_nameEdit->setText(m_Order.name());
                                  m_dateEdit->setDate(m_Order.dateAdded());
                                  m_quantitySpin->setValue(m_Order.quantity());
                                  m_priceSpin->setValue(m_Order.price());
                                  calculateCost();
                                  }
                                  //calculate the cost of an order
                                  void OrderForm::calculateCost(){
                                  double total = double(m_quantitySpin->value()* m_priceSpin->value());
                                  m_totalEdit->setText(QString::number(total, 'f', 2));
                                  }

                                  ordergui.h

                                  #ifndef ORDERGUI_H
                                  #define ORDERGUI_H
                                  #include <QMainWindow>
                                  #include <QTextEdit>

                                  class ordergui : public QMainWindow {
                                  Q_OBJECT
                                  public:
                                  ordergui();
                                  public slots:
                                  void add_order_clicked();
                                  void view_orders_clicked();
                                  private:
                                  void createMenu();
                                  void createActions();
                                  QTextEdit* m_text;
                                  QMenu* orderMenu;
                                  QAction* add_order;
                                  QAction* view_orders;
                                  };

                                  #endif // ORDERGUI_H

                                  ordergui.cpp

                                  #include <QTGui>
                                  #include "ordergui.h"
                                  #include "OrderForm.h"

                                  ordergui::ordergui() {
                                  m_text = new QTextEdit;
                                  setCentralWidget(m_text);
                                  createActions();
                                  createMenu();
                                  }

                                  void ordergui::createActions() {
                                  add_order = new QAction(("Add Order"), this);
                                  connect(add_order, SIGNAL(triggered()), this, SLOT(add_order_clicked()));

                                  view_orders = new QAction(("View Order"), this);
                                  connect(view_orders, SIGNAL(triggered()), this, SLOT(view_orders_clicked()));
                                  

                                  }

                                  void ordergui::createMenu() {
                                  orderMenu = menuBar()->addMenu("Order");
                                  orderMenu->addAction(add_order);
                                  orderMenu->addAction(view_orders);
                                  orderMenu->exec();
                                  }

                                  void ordergui::add_order_clicked() {
                                  //OrderForm* obj = new OrderForm;
                                  setCentralWidget(new OrderForm(this));
                                  }

                                  void ordergui::view_orders_clicked() {
                                  setCentralWidget(m_text);
                                  }

                                  main.cpp

                                  #include <QtGui>
                                  #include <QApplication>
                                  #include "ordergui.h"

                                  int main(int argc, char *argv[]) {
                                  QApplication app(argc, argv);
                                  ordergui order1;

                                  order1.show();
                                  return app.exec&#40;&#41;;
                                  

                                  }

                                  @

                                  [edit: modified the code to allow proper tagging SGaist]

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

                                    HI, sorry I tried to copy your messed up code here but it's almost impossible because it puts everything into one line and the quotations are also messed up etc.

                                    If you put it in proper code tags or just zip and upload your project somewhere I will try finding your error, but not like this if I have to cleanup all the code just to recreate your whole project. :D

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

                                      Hi Xander thanks for taking a look
                                      I uploaded it to dropbox, hope that's ok?

                                      download link:

                                      https://www.dropbox.com/s/i2hz6per5o78lry/Order-exercise.rar

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

                                        Ok i had to add some stuff to the pro file and include QtWidgets in OrderForm.h to compile the project, I think you are using Qt 4 or why don't you need that?

                                        Anyway I didn't change the code and it works!? lol
                                        maybe a little weird to start the app with a menu only, but when i click on "Add Order" i see this:
                                        !http://i.imgur.com/2ADRudL.png(order form)!
                                        isn't that what you want, I am a little confused now because it seems to work or what is not working!?

                                        I noticed some other bugs, but i'm waiting for your response on this first.

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

                                          Yes I am using QT 4

                                          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