How make, the result of function "press()" will be displayed on QLabel, when I press QPushButton?
-
Hello. I am a beginner in Qt. I learn Qt with course on nokia.com. I try to write a program, who when I press the QPushButton , in QLabel i will see a result of addition : 2+2. This is my code:
in header.h
@
#include<QtGui>class main: private QtWidget
{Q_OBJECT
public:
main();
private slots:
int press();
private:
QPushButton *guzik;
QLabel *tlo;
};@in header.cpp
@
#include<QtGui>
#include<iostream>
#include"header.h"using namespace std;
main::main()
{
guzik = new QPushButton;
tlo = new QLabel;
??? - how make, the result of function "press()" will be displayed on QLabel?
connect(guzik, SIGNAL(clicked()), this, SLOT(press()));
QVBoxLayout calosc;
calosc -> addWidget(&tlo);
calosc -> addWidget(&guzik);
setLayout(calosc);
}int press::main()
{
int a,b,c;
cin>>a>>b;
c=a+b;?????? - how send variable c to QPushButton ?
return c;
}@pleas , help.
-
Hi,
seems like you have more a C++ problem then a Qt problem.
Ok, first things first:I would NEVER call a class main, event if the language allows it, main is typically the method that is called by the OS and defines the application.
if you have a class CMyClass which has a mthos press, the notation is always: class::method, not method::class
you have member variables to access the label, just use them.
solution for your code:
@
#include<QtGui>class CMyClass: public QWidget // <-- changed--
{
Q_OBJECT
public:
main();
private slots:
void press(); // <-- changed
private:
QPushButton *guzik;
QLabel *tlo;
};
@@
#include <QtGui>
#include <iostream>
#include "header.h"using namespace std;
main::main()
{
guzik = new QPushButton;
tlo = new QLabel;
connect(guzik, SIGNAL(clicked()), this, SLOT(press()));
QVBoxLayout* calosc = new QVBoxLayout; // <-- changed
calosc -> addWidget(&tlo);
calosc -> addWidget(&guzik);
setLayout(calosc);
}void press::main()
{
// in a UI application, you can't read the command line. You must use dialogs for this
// int a,b,c;
// cin>>a>>b;
// c=a+b;
tlo->setText(QString::number(2+2));
return c;
}
@The code is just written directly here, not compiled nor tested.
-
Several issues here:
Write @ int main::press() @ on line 19
main::press() will be called when you press the button. To set value of "c" in the label, write: @ tlo->setText(QString::number(c)); @
You are writing a GUI application, so using "cin" is not what you would normally use. Get rid of that. For a starter, just hard-code some text/ number to display in the label. When that works, you can proceed with adding a QSpinBox or QLineEdit, and then specifying numbers to add on runtime
Also, I would personally not name the class "main". First, the general, and useful, convention is to start class names with upper case letter (like "Main"); second, remember that in C/ C++ "main" is the name of main routine, so using the same name for a class can easily lead to name clashes; third - meaningful names are usually best to use
And the last thing, not a biggie, but kind of important - write everything, including variable names, in English (American). Of course, this is not a requirement, compiler will understand any valid name, but it helps later in life, when you want to show your code to other people, or work in an international environment. I'm Polish, so I understood the names of your variables, but others here might not
-
Ah, I see Gerolf was quicker and more throughout :)
-
ok thanks :) so:
-
C++ , what I have in school and compiled in g++ or DevC++ is using in a console, and libraries like <include> wouldn't helpful here? In UI are new gramar like QApplication and older like cin, cout and other weren't use? So, when I use in school a Visual C++, gramar would be new to?
-
If I will try start my application in other operating system, like Windows or Mac, it will be run? Do I must install Qt on that machine?
Sorry for that Question, maybe are funny, but I'm on first year, and we have programming one a week.
Thanks for help :)
-
-
Grabbing a few books on the subject (both C++ and Qt) is the best way to go, then.
As for your questions:
Ad. 1. Qt is still C++. It provides over a thousand classes (QApplication is one of them - it's not part of C++ "grammar", if you like, but is available once you #include <QtCore> or <QApplication>), and is aimed at GUIs. Developing console applications is still possible - and quite common - with Qt. cin and cout are good for console applications (hence the name: "console input" and "console output"), but won't be much help in GUI apps. In GUIs, we use various input widgets (like QSpinBox, QTextEdit, and a lot more - take a look inside Qt Creator or Qt Designer to get a list) to get some data from users, and display information in a lot of ways (QLabel, QWebView, QMenu, QML,... it's a looooong list).
I would say that g++ and DevC++ are quite hard for beginner development in Qt, use Qt SDK instead - it features a gorgeous IDE, Qt Creator, lots of examples, demos, wizards, and a built-in documentation (which is one of the best framework documentations available). This should make your life easier - all of the hardcore stuff will be done automatically, so there will be less frustration in the beginning.
Ad. 2. Yes, Qt is fully cross-platform, so you can compile and run on any Unix-like OS and Windows. And yes, you have to install Qt on target machine (or bundle it with your app, or require it as a dependency... sorry but there are many options available).
Don't be sorry, as long as you are eager to learn, and keep asking precise questions, we'll be happy to help :)
-
Sorry for being so talkative, but here are some further thoughts:
Once a week is too rare to truly learn programming. I suggest you do more than is required - on your own. Will help you in later years of your study, too.
After you get some basics from books/ tutors, get heavily onto building own applications. They don't have to be big or outstandingly functional, but will help you learn (file operations, regular expressions, databases, web interaction etc.).