Results 1 to 7 of 7

Thread: problems in a small dialog

  1. #1
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default problems in a small dialog

    I wanted to create a small dialog that using buttons we can add some numbers.
    First I designed a form by Qt Designer using Dialog Without Buttons template like this:

    1.jpg

    Then I wrote a calculator.h file like this:


    Qt Code:
    1. #ifndef CALCULATOR_H
    2. #define CALCULATOR_H
    3.  
    4. #include<QDialog>
    5. #include "ui_Calculator.h"
    6.  
    7. class Calculator : public QDialog, public Ui::Calculator
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Calculator(QWidget* parent = 0);
    13.  
    14. private slots:
    15. void myslot();
    16. };
    17.  
    18. #endif // CALCULATOR_H
    To copy to clipboard, switch view to plain text mode 


    Then, a calculator.cpp this way:


    Qt Code:
    1. #include <QtWidgets>
    2. #include "calculator.h"
    3.  
    4. Calculator::Calculator(QWidget *parent)
    5. :QDialog(parent)
    6. {
    7. setupUi(this);
    8.  
    9. connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot()));
    10. }
    11.  
    12. void Calculator::myslot(){
    13. lineEdit -> setText("1");
    14. }
    To copy to clipboard, switch view to plain text mode 


    And finally the main.cpp this way:

    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3.  
    4. #include "ui_Calculator.h"
    5.  
    6. int main(int argc, char* argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. Ui::Calculator ui;
    11. QDialog* dialog = new QDialog;
    12. ui.setupUi(dialog);
    13. dialog -> show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Now the code runs without any error but not as expected! That is when I click on '1' button nothing will be printed out onto the lineEdit.
    Why please and how to solve it?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problems in a small dialog

    You main() never creates an instance of Calculator, so there is no chance any of its could could ever be executed.

    Instead of all the code between the QApplication creation and the return line, create an object of type Calculator and show it.

    Cheers,
    _

    P.S.: additional suggestion would be to use the generated class as a member instead of inheriting from it

  3. #3
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: problems in a small dialog

    Do you mean instead of:
    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. Ui::Calculator ui;
    5. QDialog* dialog = new QDialog;
    6. ui.setupUi(dialog);
    7. dialog -> show();
    8.  
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    I should write:

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. Calculator calc;
    6. calc.show();
    7.  
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    ?

    Can it be running?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problems in a small dialog

    Yes, that is exactly what I meant and what you are intending to do.

    Cheers.
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: problems in a small dialog

    Thanks for the reply.
    I made that changes but I got these errors:

    1- C:\Users\Me\Documents\Qt\Calculator\main.cpp:9: error: 'Calculator' was not declared in this scope
    Calculator calc;
    ^

    2- C:\Users\Me\Documents\Qt\Calculator\main.cpp:10: error: 'calc' was not declared in this scope
    calc.show();
    ^

  6. #6
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problems in a small dialog

    Hi, you need to #include "calculator.h" in main.cpp

    Ginsengelf

  7. The following user says thank you to Ginsengelf for this useful post:

    franky (20th October 2016)

  8. #7
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: problems in a small dialog

    Thank you.The problem is solved.

Similar Threads

  1. Problems rendering small border-image in Windows
    By droneone in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2013, 16:05
  2. Center dialog on screen problems
    By lalesculiviu in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2009, 13:15
  3. Performance problems with small pixmap
    By RThaden in forum Qt Programming
    Replies: 4
    Last Post: 9th July 2008, 15:14
  4. Problems updating a dialog
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 1st April 2006, 10:17
  5. Problems with a dialog
    By SkripT in forum Qt Programming
    Replies: 16
    Last Post: 29th March 2006, 13:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.