Results 1 to 7 of 7

Thread: Simple Math Calculation Questions in QT (PyQt, Qt4 C++)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Simple Math Calculation Questions in QT (PyQt, Qt4 C++)

    Okay, I have been working for a few hours and here is what I have so far. It is a bit different from what you had. For background, I created buttons in the mainwindow.ui and they are labeled as follows: lineEditA, lineEditB, and lineEditSum. The button I used was a tool button and I named it calculateButton. Instead of doing all of that original formula, I am just trying to do a simple a - b = sum now just so I can get the hang of that and then work up from there. I am getting some errors while running this code. I will explain the errors after I show the code.

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private slots:
    19.  
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. void onCalculateClicked();
    24. void calculateAndDisplaySum();
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include <QLineEdit>
    2. #include <QToolButton>
    3. #include "mainwindow.h"
    4. #include "ui_mainwindow.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. connect(ui->calculateButton, SIGNAL(clicked()), this, SLOT(onCalculateClicked()));
    12.  
    13. //onCalculateClicked(); -- Are these necessary?
    14. //calculateAndDisplaySum();
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::onCalculateClicked()
    23. {
    24. calculateAndDisplaySum();
    25. }
    26.  
    27. void MainWindow::calculateAndDisplaySum()
    28. {
    29. double a = lineEditA->text().toDouble();
    30. double b = lineEditB->text().toDouble();
    31. double sum = a - b;
    32.  
    33. lineEditSum->setText(QString::number(sum, 'g', 2));
    34. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.ui
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>400</width>
    10. <height>300</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <widget class="QLineEdit" name="lineEditSum">
    18. <property name="geometry">
    19. <rect>
    20. <x>120</x>
    21. <y>10</y>
    22. <width>31</width>
    23. <height>20</height>
    24. </rect>
    25. </property>
    26. </widget>
    27. <widget class="QLineEdit" name="lineEditA">
    28. <property name="geometry">
    29. <rect>
    30. <x>10</x>
    31. <y>10</y>
    32. <width>31</width>
    33. <height>20</height>
    34. </rect>
    35. </property>
    36. </widget>
    37. <widget class="QLineEdit" name="lineEditB">
    38. <property name="geometry">
    39. <rect>
    40. <x>60</x>
    41. <y>10</y>
    42. <width>31</width>
    43. <height>20</height>
    44. </rect>
    45. </property>
    46. </widget>
    47. <widget class="QLabel" name="label">
    48. <property name="geometry">
    49. <rect>
    50. <x>50</x>
    51. <y>10</y>
    52. <width>16</width>
    53. <height>16</height>
    54. </rect>
    55. </property>
    56. <property name="text">
    57. <string>-</string>
    58. </property>
    59. </widget>
    60. <widget class="QLabel" name="label_2">
    61. <property name="geometry">
    62. <rect>
    63. <x>100</x>
    64. <y>10</y>
    65. <width>16</width>
    66. <height>16</height>
    67. </rect>
    68. </property>
    69. <property name="text">
    70. <string>=</string>
    71. </property>
    72. </widget>
    73. <widget class="QToolButton" name="calculateButton">
    74. <property name="geometry">
    75. <rect>
    76. <x>40</x>
    77. <y>40</y>
    78. <width>71</width>
    79. <height>19</height>
    80. </rect>
    81. </property>
    82. <property name="text">
    83. <string> Calculate</string>
    84. </property>
    85. </widget>
    86. </widget>
    87. <widget class="QMenuBar" name="menuBar">
    88. <property name="geometry">
    89. <rect>
    90. <x>0</x>
    91. <y>0</y>
    92. <width>400</width>
    93. <height>21</height>
    94. </rect>
    95. </property>
    96. </widget>
    97. <widget class="QToolBar" name="mainToolBar">
    98. <attribute name="toolBarArea">
    99. <enum>TopToolBarArea</enum>
    100. </attribute>
    101. <attribute name="toolBarBreak">
    102. <bool>false</bool>
    103. </attribute>
    104. </widget>
    105. <widget class="QStatusBar" name="statusBar"/>
    106. </widget>
    107. <layoutdefault spacing="6" margin="11"/>
    108. <resources/>
    109. <connections/>
    110. </ui>
    To copy to clipboard, switch view to plain text mode 
    Okay, so now that's out of the way, just in case you want to compile it in QtCreator5.5 yourself, the error messages only show up on the mainwindow.cpp and they are as follows:

    mainwindow.cpp:28: error: C2065: 'lineEditA' : undeclared identifier
    mainwindow.cpp:28: error: C2227: left of '->text' must point to class/struct/union/generic type
    type is 'unknown-type'
    mainwindow.cpp:28: error: C2228: left of '.toDouble' must have class/struct/union

    mainwindow.cpp:29: error: C2065: 'lineEditB' : undeclared identifier
    mainwindow.cpp:29: error: C2227: left of '->text' must point to class/struct/union/generic type
    type is 'unknown-type'
    mainwindow.cpp:29: error: C2228: left of '.toDouble' must have class/struct/union

    mainwindow.cpp:32: error: C2065: 'lineEditSum' : undeclared identifier
    mainwindow.cpp:32: error: C2227: left of '->setText' must point to class/struct/union/generic type
    type is 'unknown-type'

    That should be it. Sorry for all the information and bombardment of code. Thank you for all the help!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Simple Math Calculation Questions in QT (PyQt, Qt4 C++)

    Error #1 - move the declaration of your slot method (onCalculateClicked()) to the "private slots:" section of the MainWIndow class definition.

    Remaining errors all stem from the fact that "lineEditA", etc. are *not* member variables of your MainWindow class, but are member of the Ui::MainWindow class. This is a class automatically created by Qt's MOC (metaobject compiler) from your .ui file. It lives in the Ui namespace, which is why it is referenced as "Ui::MainWindow". Your own MainWindow constructor creates an instance of this Ui class (line 8 of mainwindow.cpp) and then initializes it (setupUi()) in the body of the constructor.

    Since all of the widget variables defined in your .ui file are public member variables of the Ui::MainWindow class, so to retrieve them from code in MainWindow, you have to go through the pointer to the Ui instance:

    ui->lineEditA->text(), etc.

    It is a good habit to learn to embed your child widgets in a layout of some sort so they will behave properly on resize. Your centralWidget doesn't use a layout, and you've hard-coded all of the child widget positions within it. While this may work in this case, it isn't good practice. It is good to get into the habit of putting a layout into every custom widget so that good things happen on resize, change of font, whatever.

    In this case, I would add a grid layout, and use horizontal and vertical spacers to push things into place. Here's your mainwindow.ui reworked with a grid layout and spacers:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>445</width>
    10. <height>203</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <layout class="QGridLayout" name="gridLayout">
    18. <item row="0" column="5">
    19. <spacer name="horizontalSpacer">
    20. <property name="orientation">
    21. <enum>Qt::Horizontal</enum>
    22. </property>
    23. <property name="sizeHint" stdset="0">
    24. <size>
    25. <width>40</width>
    26. <height>20</height>
    27. </size>
    28. </property>
    29. </spacer>
    30. </item>
    31. <item row="0" column="4">
    32. <widget class="QLineEdit" name="lineEditSum"/>
    33. </item>
    34. <item row="1" column="2">
    35. <widget class="QToolButton" name="calculateButton">
    36. <property name="text">
    37. <string> Calculate</string>
    38. </property>
    39. </widget>
    40. </item>
    41. <item row="2" column="2">
    42. <spacer name="verticalSpacer">
    43. <property name="orientation">
    44. <enum>Qt::Vertical</enum>
    45. </property>
    46. <property name="sizeHint" stdset="0">
    47. <size>
    48. <width>20</width>
    49. <height>40</height>
    50. </size>
    51. </property>
    52. </spacer>
    53. </item>
    54. <item row="0" column="0">
    55. <widget class="QLineEdit" name="lineEditA"/>
    56. </item>
    57. <item row="0" column="1">
    58. <widget class="QLabel" name="label">
    59. <property name="text">
    60. <string>-</string>
    61. </property>
    62. </widget>
    63. </item>
    64. <item row="0" column="3">
    65. <widget class="QLabel" name="label_2">
    66. <property name="text">
    67. <string>=</string>
    68. </property>
    69. </widget>
    70. </item>
    71. <item row="0" column="2">
    72. <widget class="QLineEdit" name="lineEditB"/>
    73. </item>
    74. </layout>
    75. </widget>
    76. <widget class="QMenuBar" name="menuBar">
    77. <property name="geometry">
    78. <rect>
    79. <x>0</x>
    80. <y>0</y>
    81. <width>445</width>
    82. <height>26</height>
    83. </rect>
    84. </property>
    85. </widget>
    86. <widget class="QToolBar" name="mainToolBar">
    87. <attribute name="toolBarArea">
    88. <enum>TopToolBarArea</enum>
    89. </attribute>
    90. <attribute name="toolBarBreak">
    91. <bool>false</bool>
    92. </attribute>
    93. </widget>
    94. <widget class="QStatusBar" name="statusBar"/>
    95. </widget>
    96. <layoutdefault spacing="6" margin="11"/>
    97. <resources/>
    98. <connections/>
    99. </ui>
    To copy to clipboard, switch view to plain text mode 

    As you expand on this in Qt Designer, you add new widgets to the grid layout by clicking and dragging from Designer's "Widget Box" onto the form. The place where the widget will end up when you drop it will be highlighted by a red box (if you add it to an empty box in the grid) or a horizontal or vertical blue line if dropping it will expand the grid to add a new row or column of cells.

Similar Threads

  1. Few (hopefully) simple questions
    By QTGuy72 in forum Newbie
    Replies: 1
    Last Post: 10th July 2012, 17:30
  2. Simple questions
    By assismvla in forum Newbie
    Replies: 3
    Last Post: 2nd May 2010, 01:57
  3. may i ask questions related to pyqt here???
    By pyqt123 in forum Newbie
    Replies: 2
    Last Post: 14th December 2009, 06:28
  4. Two probably simple Qt4 Questions
    By frenk_castle in forum Newbie
    Replies: 3
    Last Post: 16th September 2009, 15:37
  5. Simple DB app, some questions.
    By juannm in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2008, 11:46

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.