Some errors in a simple program
-
Hi all,
Please consider these.h
,.cpp
andmain,cpp
files:test.h:
#ifndef TEST1_H #define TEST1_H #include <QMainWindow> class QlineEdit; class QPushButton; class test1 : public QMainWindow { Q_OBJECT public: test1(QWidget* parent = 0); private slots: void show_clicked(); private: int i; QlineEdit* line; QPushButton* show; QPushButton* quit; }; #endif // TEST1_H
test1.cpp:
#include <QtWidgets> #include "test1.h" test1::test1(QWidget* parent) : QMainWindow(parent) { i = 5; show = new QPushButton(tr("show")); quit = new QPushButton(tr("Quit")); line = new QlineEdit; connect(show, SIGNAL(clicked(bool)), line, SLOT(show_clicked())); connect(quit, SIGNAL(clicked(bool)), this, SLOT(close)); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(line); layout->addWidget(show); layout->addWidget(quit); setLayout(layout); } //********************************** void test1::show_clicked() { line->text(i); }
and main.cpp:
#include<QApplication> #include "test1.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); test1* t1 = new test1; t1->show(); return app.exec(); }
I get these errors in the test1.cpp:
test1.cpp:10: error: invalid use of incomplete type 'class QlineEdit'
line = new QlineEdit;test1.cpp:12: error: no matching function for call to 'test1::connect(QPushButton&, const char*, QlineEdit*&, const char*)'
connect(show, SIGNAL(clicked(bool)), line, SLOT(show_clicked()));*
^
error: no type named 'Object' in 'struct QtPrivate::FunctionPointer<const char>'*no matching function for call to 'QVBoxLayout::addWidget(QlineEdit&)'
layout->addWidget(line);*test1.cpp:26: error: invalid use of incomplete type 'class QlineEdit'
line->text(i);5 errors!
-
@tomy said in Some errors in a simple program:
invalid use of incomplete type 'class QLineEdit'
Means that you need its include file.
It simply only knows its name, not full info.so you need
#include <QlineEdit>Also, you have a misstype
line = new QlineEdit;its with big L
line = new QLineEdit;
so its most likely that.
-
@mrjj
Because of forward declarations, it didn't need more includes. I corrected the typos and now it's fine except a way to show an int number in a lineEdit.
I tried to find a function (of lineEdit) for that but couldn't find one!Thank you for your help.
-
@tomy
To show an int as a string, you can use QString::numberLike
lineEdit->setText( QString::number( int_variable ) );Note that it wont do right if other locales etc. But I will assume
for your tests, we can ignore that for now. -
Thank you.
I din't understand your comment on not using int values for a lineEdit. Would you please explain it?
If a lineEdit is only good to show texts, what is the standard way to show values on programs, please?I changed the QPushbutton show to show_number. Then ran the program. It ran successfully but shows only a window!!
Isn't the setLayout() function sufficient to map the widgets on the window when running/showing? -
- I din't understand your comment on not using int values for a lineEdit. Would you please explain it?
Its not about the lineEdit. using ::number converts directly to
a text version of the number.
like 7 -> "7".
However, for some other uses, you must take into account how its written. Some countries uses . for thousand like 1.000, other might use , (comma) and so on. 1,000
Qt has other function to convert such numbers besides ::number.
That was what i meant. -
-
@tomy
This class
http://doc.qt.io/qt-5/qlocale.htmlsetLayout(layout); should do it but else try to give widget as parent in ctor
QVBoxLayout* layout = new QVBoxLayout(this); << give parent
and dont do setLayout -
This class
http://doc.qt.io/qt-5/qlocale.htmlFor showing a number?!
setLayout(layout); should do it but else try to give widget as parent in ctor
QVBoxLayout* layout = new QVBoxLayout(this); << give parent
and dont do setLayoutI declared it like this:
QVBoxLayout* layout = new QVBoxLayout(this);
and didn't use thesetLayout(layout);
.
But no changes.
I think the problem is very simple. Please take a look at the codes once gain:#ifndef TEST1_H #define TEST1_H #include <QMainWindow> class QLineEdit; class QPushButton; class test1 : public QMainWindow { Q_OBJECT public: test1(QWidget* parent = 0); private slots: void show_clicked(); private: int i; QLineEdit* line; QPushButton* show_number; QPushButton* quit; }; #endif // TEST1_H
#include <QtWidgets> #include "test1.h" test1::test1(QWidget* parent) : QMainWindow(parent) { i = 5; show_number = new QPushButton(tr("show")); quit = new QPushButton(tr("Quit")); line = new QLineEdit; connect(show_number, SIGNAL(clicked(bool)), line, SLOT(show_clicked())); connect(quit, SIGNAL(clicked(bool)), this, SLOT(close)); QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(line); layout->addWidget(show_number); layout->addWidget(quit); setLayout(layout); } //********************************** void test1::show_clicked() { line->setText(QString::number(i)); }
#include<QApplication> #include "test1.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); test1* t1 = new test1; t1->show(); return app.exec(); }
-
@tomy said in Some errors in a simple program:
For showing a number?!
For correctly formatting numbers as string while considering country norms.
- think the problem is very simple. Please take a look at the codes once gain:
Ah its because its a MainWindow
it has special center widget called
http://doc.qt.io/qt-5/qmainwindow.html#centralWidget
which you should set the layout on.
and not the mainwindow itself!!QVBoxLayout* layout = new QVBoxLayout(this->centralWidget());
layout->addWidget(line);
layout->addWidget(show_number);
layout->addWidget(quit);
setLayout(layout);if used MUST be centralWidget()->setLayout(xx) -
QVBoxLayout* layout = new QVBoxLayout(this->centralWidget());
layout->addWidget(line);
layout->addWidget(show_number);
layout->addWidget(quit);
setLayout(layout);if used MUST be centralWidget()->setLayout(xx)Hi,
I used both cases but neither worked for QMainWindow. I had to change it to QDialog. then it worked and showed the widgets.
Incidentally, one of my connects in the code was:
connect(show_number, SIGNAL(clicked(bool)), line, SLOT(show_clicked()));
When I clicked on the show button, nothing was shown in the lineEdit. But when I changed the receiver from line to
this
, it showed 5. Why please? -
@tomy said in Some errors in a simple program:
But when I changed the receiver from line to this, it showed 5. Why please?
Well, maybe before the connect was not right.
so if line didnt have show_clicked()
then connect returns false and no connection is made.