I think you mentioned you have the book "GUI Programming with Qt4". Did you read the second chapter? Especially sections "Subclassing QDialog" and "Rapid Dialog Design"? These explain how to do what you're trying to do.
Printable View
I think you mentioned you have the book "GUI Programming with Qt4". Did you read the second chapter? Especially sections "Subclassing QDialog" and "Rapid Dialog Design"? These explain how to do what you're trying to do.
No, but I'll read it ;) Ok, so I will look at it
Don't have a look. Read it.
And after it I will know how to do it...I would like to know ;)
Yes. You're asking about very simple things and it seems that you even didn't look at the documentation or the book. We won't teach you C++ here. If you have trouble with C++ I suggest you also take a look at our links section and download (and read, at least parts of it) "Thinking in C++".
I'd like the Chapter 2 to be simplier than designer using a component? ;) But there isn't much to do with the code in post #50, right?
Read and understand the chapter. We can give you the exact code for your problem but then you won't learn anything and you'll come right back with a simmilar issue.
But in book, there appear one more line after QApplication app... It's
which not appear in my application, but it works.Code:
Ui::MainWindow ui
Hi than. I read chapter 2, I understand it, but I still don't know how to connect action to another dialog. It shows how to make slots and use it, but not how to connect two different dialogs. Regards
I want to connect action in my menu called actionAbout to dialog which is in ui_about
if a ucer clicks a button, than about dialog is displayed
Right
And if I have a dialog included to my file called Dialog, so the OtherDialog will the replaced by Dialog? And what about "dlg"? and must on_aboutAction_triggered() be defined in slots section? I mean the same way that I do it with other functions?Code:
void MainWindow::on_actionAbout_triggered() { OtherDialog dlg( this ); dlg.exec(); }
Yes, you have to replace it with the name of a class you want to use.
It doesn't matter, it's just a variable.
Yes, it has to be a slot.
As a homework, please, get a copy of C++ Primer (Polish edition is called Podstawy języka C++) by S. Lippman and J. Lajoie and read parts 2--5.
;) And to connect dialog to a slot, I have to do this, what is described in posts #65, 66
and also include this ui_about.h dialog file and nothing else? Will it be done correctly?
You don't connect a dialog to a slot. You connect a signal triggered() of the action actionAbout to a slot that has code which creates the dialog object and calls exec() on it to show it as modal.
Your absolutely right.
There's a problem. I have those files:
Maker.cpp
Maker.hCode:
#include <QtGui> #include "maker.h" { ui.setupUi(this); } void MainWindow::on_actionAbout_triggered() { Dialog dlg( this ); dlg.exec(); }
ui_about.hCode:
#ifndef MAKER_H #define MAKER_H #include "ui_maker.h" #include "ui_about.h" { Q_OBJECT public: private slots: void on_actionAbout_triggered(); private: Ui::MainWindow ui; }; #endif
And if I try to compile it, there appear errors:Code:
#ifndef UI_ABOUT_H #define UI_ABOUT_H #include <QtCore/QVariant> #include <QtGui/QAction> #include <QtGui/QApplication> #include <QtGui/QButtonGroup> #include <QtGui/QDialog> #include <QtGui/QLabel> #include <QtGui/QPushButton> class Ui_Dialog { public: QLabel *label; QLabel *label_2; QLabel *label_3; QPushButton *pushButton; { label->setLayoutDirection(Qt::LeftToRight); label->setTextFormat(Qt::AutoText); label_2->setLineWidth(1); label_2->setMidLineWidth(0); label_2->setTextFormat(Qt::RichText); label_2->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop); label_2->setWordWrap(true); pushButton->setDefault(true); pushButton->setFlat(false); retranslateUi(Dialog); size = size.expandedTo(Dialog->minimumSizeHint()); Dialog->resize(size); } // setupUi { Dialog->setAccessibleName(QApplication::translate("Dialog", "About", 0, QApplication::UnicodeUTF8)); label->setText(QApplication::translate("Dialog", "About TimeTable Maker", 0, QApplication::UnicodeUTF8)); label_3->setText(QApplication::translate("Dialog", "Thank you for using TimeTable Maker", 0, QApplication::UnicodeUTF8)); Q_UNUSED(Dialog); } // retranslateUi }; namespace Ui { class Dialog: public Ui_Dialog {}; } // namespace Ui #endif // UI_ABOUT_H
maker.cpp - in member function 'void MainWindow:: on_actionAbout_triggered()':
maker.cpp:line 16: Dialog undeclared (first use)
maker.cpp:line 16: Each undeclared identifier is reported only once for each function it appears in
maker.cpp:line 16: Expected ; before 'dlg'
maker.cpp:line 20: 'dlg' undeclared (first use)
What's wrong? I've done everything how you'd described!:(
Do you have a class called "OtherDialog"?
So if you don't have such classes, why are you using them? You have to create a subclass of QDialog that uses your uic generated code to initialise itself which is explained in the link you're constantly being pointed to.
Please, understand, there is nothing magical going on here. If you don't implement a class, you can't use it - it won't be created out of thin air. I asked you about your C++ knowledge and I was given an empty answer, but from what I see you don't know much about it. So I repeat Jacek's suggestion to take a good book about C++, be it either C++ Primer, Thinking in C++ (available online) or even Symfonia C++.
The problem here is that if we don't give you exact code you are to copy and paste into your files, there is no way you're going to obtain the result you want, because you don't seem to try to do anything by yourself. It seems that you know what to do, because you implemented the exact same thing that is required here with the main window. I just can't seem to understand why don't you repeat the exact same approach with the dialog.
We're all trying to teach you something here, but it seems we're doing something wrong, so please explain us what exactly you don't know how to do and I'm speaking of some fundamental things - like subclassing, creating variables, single or multiple inheritance, etc. Is this all known to you? Do you have any trouble handling those basics?
Great, now we're getting somewhere. Please, if at any time you don't understand what we say (any term, code, etc.) just say so and we'll explain it.
Yes. This is exactly the same situation - you have a UI definition in one class and you want to apply it to a widget class (like QDialog or QMainWindow).Quote:
Do I have to subclass this Dialog in the same way I was subclassing MainWindow?
Yes, but I suggest you use separate pair of files for that. You'll get cleaner code and faster compilation.
But in this case (subclassing QDialog) I don't define slots, do I?
Do you need any?
I suppouse no, because I'd defined all importand slots when I was subclassing MainWindow
So the answer is no, you don't define slots :)
Thanks:) And I've got one more question, why some slots are defined in private slots and some are defined in private? Both are private sections of MainWindow class, what's the difference?
C++ access qualifiers public, protected and private (refer to you favourite C++ book for more explanation) apply to slots as well.
A slot is not a C++ but a Qt term. A normal C++ function is not a slot but a slot is a function. That is, a normal C++ function cannot be used as a slot in a connect statement but a slot can be called like a normal C++ function. A function needs to be explicitly declared as a slot to be able to use it as a slot. So there are no "slots defined in private slots" and "slots defined in private". Only the former are slots.
So we define slots in private slots, and standard cpp functioms in private, right?
Yes, that's right.
And in maker.cpp I have to do the same what I was doing with MainWindow:
but without implementing slots, right?Code:
{ ui.setupUi(this); } void MainWindow::on_actionAbout_triggered() { Dialog dlg( this ); dlg.exec(); }
I have a problem. I've implemented this:
Maker.h
Code:
#ifndef MAKER_H #define MAKER_H #include "ui_maker.h" { Q_OBJECT public: private slots: void on_actionAbout_triggered(); private: Ui::MainWindow ui; }; #endif
About.h
Code:
#ifndef ABOUT_H #define ABOUT_H #include "ui_about.h" { Q_OBJECT public: private: Ui::Dialog ui; }; #endif
Maker.cpp
Code:
#include <QtGui> #include "maker.h" #include "about.h" { ui.setupUi(this); } void MainWindow::on_actionAbout_triggered() { Dialog dlg( this ); dlg.exec(); } { ui.setupUi(this); }
And also main.cpp, which contents I won't post, because it's obvious. And there appear errors:
maker.cpp:in member function 'void MainWindow::on_actionAbout_triggered()':
maker.cpp:17:error: no matching function for call to 'Dialog::Dialog(MainWindow* const)
about.h:9:note: candidates are Dialog::Dialog(const Dialog&)
about.h:13:note: Dialog::Dialog(QDialog*)
What's wrong? I thought I'd done everything as it was described in C++ GUI and in this topic. Regards
p.s.
Forum mechanism replaced : D with a smile and also : o with the other smile, as you seen so far
It should be:
The dialog of yours demands a QDialog as a parent. QMainWindow is not a QDialog but both are QWidgets.
I replaced QDialog with QWidget but it didn't solve the problem:
maker.cpp:31:error: Prototype for Dialog::Dialog(QDialog*) does not match any in class Dialog
about.h:9:error: candidates are Dialog:Dialog(const Dialog&)
about:.h:13:error Dialog::Dialog(QWidget*)
What's wrong now? Regards
Most likely you forgot to change it in the .cpp file too.
Yes, now I changed it into cpp file too, but there appear error:
maker.cpp: In constructor Dialog::Dialog(QWidget*)
maker.cpp:31:error: Type 'class QWidget' is not a direct base of Dialog
What's wrong, now? Regards,
Edit: No, wait. I don't want to spoon-feed the solution.
A parent and a base class are different things. You changed the parent correctly, but you are now calling a wrong base class. Just think of it yourself. ;)
I forgot to change one of QDialog to QWidget, but there still are problems;
here is original log of compilation:
http://allyoucanupload.webshots.com/...70322826506289
Do you know why it doesn't work?
Does class "Dialog" inherit QDialog?
Yes, I think so. Here are the files
Maker.h
Code:
#ifndef MAKER_H #define MAKER_H #include "ui_maker.h" { Q_OBJECT public: private slots: void on_actionAbout_triggered(); private: Ui::MainWindow ui; }; #endif
About.h
Code:
#ifndef ABOUT_H #define ABOUT_H #include "ui_about.h" { Q_OBJECT public: private: Ui::Dialog ui; }; #endif
Maker.cpp
And also main.cpp, ui_about.h, ui_maker.h which contents are obvious. So, what's wrong in it? RegardsCode:
#include <QtGui> #include "maker.h" #include "about.h" { ui.setupUi(this); } void MainWindow::on_actionAbout_triggered() { Dialog dlg( this ); dlg.exec(); } { ui.setupUi(this); }
The compiler told you what's wrong.
Dialog isn't derived directly from QWidget, so you can't invoke QWidget constructor, so this:
should be:
I've done it, but it didn't solve the problem. Compilation log:
http://allyoucanupload.webshots.com/...74960843080783
Regards