Simple project new Widget-Setting layout via code
-
Hi all
i'm wondering how to set a QVBoxLayout to a pre-generated Widget:// header #ifndef FINANZE_H #define FINANZE_H #include <QWidget> #include <QtWidgets> namespace Ui { class Finanze; } class Finanze : public QWidget { Q_OBJECT public: explicit Finanze(QWidget *parent = 0); ~Finanze(); private: Ui::Finanze *ui; }; #endif // FINANZE_H
// cpp #include "finanze.h" #include "ui_finanze.h" Finanze::Finanze(QWidget *parent) : QWidget(parent), ui(new Ui::Finanze) { ui->setupUi(this); QPushButton *btn1 **= new QPushButton; QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(btn1); Finanze.setLayout(layout); // Why i can't do something like that? }``` Finanze::~Finanze() { delete ui; }
-
Finanze.setLayout(layout); // Why i can't do something like that?
You can, unless there already is a layout set, which is highly likely if you have a .ui file. The layout was probably set in the designed and the
setupUi(this)
is applying what you set in the designer. You can't have two layouts set on the same widget.
If you want to create the UI manually from code (as you seem to) then you don't need the .ui file and all the related code. -
The finanze.ui haven't any layout at project start, he give me an error when i try to do Finanze.setLayout(layout)
error: expected unqualified-id before '.' token
Finanze.setLayout(layout);
^Isn't finanze a widget?
-
Ah, sorry, got off track.
Finanze is a class name. You can't call a method on a class name in c++.
Either just callsetLayout(layout)
(which will implicitly resolve the call for the current instance), or, if you want to be very explicit, callthis->setLayout(layout)
, although there's litle value in that.Btw. If there's no layout set in that .ui file then why do you need it at all?
-
So if i write in the cpp of a class, i declare the STANDARD thing that class contain? Everytime i create a new "class finance" , if i setted a layout with button, they will show me the same layout.
But i can still operate in this created class in the main, right? Example i create an istance of Finance class called "Hello", if i want to set a new layout to "Hello" or add a new button, i can do that? Even if Finance class already have a layout?Like u saw, i'm just coding for learning, but almost every 2 line of code i have to stop because can't find what i'm wrong or what i have to do!
So basically i roam through the web searching for a solution, if i can't find then i try to post it here :D
I have the ui file just because i've created it not on purpose, just did something wrong following a tutorial...i saw that u're so present on this board, so ill try to get the best from you :D, i had another problem yesterday, following a tutorial to create a notepad:
#ifndef NOTEPAD_H #define NOTEPAD_H #include <QApplication> #include <QtWidgets> #include <QWidget> class Notepad : public QWidget { Q_OBJECT public: explicit Notepad(QWidget *parent = 0); ~Notepad(); private slots: void quit(); private: QTextEdit *textEdit; QPushButton *btnQuit; }; #endif // NOTEPAD_H
and this is the cpp
#include "notepad.h" Notepad::Notepad(QWidget *parent) : QWidget(parent) { textEdit = new QTextEdit; btnQuit = new QPushButton(tr("esci")); connect(btnQuit,SIGNAL(clicked(bool)),this,SLOT(quit())); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(textEdit); layout->addWidget(btnQuit); setLayout(layout); setWindowTitle(tr("Notepad")); } Notepad::~Notepad() { }
But when i try to build it, he say to me that there is an undefined reference to `Notepad::quit()'
Why?
-
So if i write in the cpp of a class, i declare the STANDARD thing that class contain? Everytime i create a new "class finance"
You don't declare any standard things. You should familiarize yourself with the following c++ concepts: class, class declaration, class definition and class instance. These are different concepts and you seem to struggle with figuring out what is what.
In short - in a header you declare a class (i.e. describe what it can do). In .cpp you define that class (i.e. describe how it does that by giving a body to the methods and variables declared in the header). In other files (for example in main.cpp) you instantiate a class, i.e. create an object that is described by the declaration in the header and behaves as defined in the cpp. The constructor (
Finanze::Finanze(QWidget *parent)
) of a class describes what happens when you create an instance of it. Inside methods of an object (constructor is a method too) you refer to the instance of it (this
) and not to the name of the class. So you don't set a layout on a class (Finanze.setLayout
). You set it on an instance of a class (this->setLayout
).For example
//Finanze.h class Finanze : public QWidget { public: Finanze(QWidget* parent = 0); }; //Finanze.cpp Finanze::Finanze(QWidget* parent) : QWidget(parent) { setLayout(new QVBoxLayout()); } //main.cpp Finanze foo ; //this is one instance of class Finanze, a constructor is called for it here Finanze bar; //this is another instance of class Finanze, , a constructor is called for it here foo.setLayout(new QHboxLayout()); // ERROR: foo already has a layout set in the constructor foo.layout()->addWidget()new QPushButton("Hi")); //but you can add something to it if you want to delete foo.layout(); //...or delete the layout. Note that it does not delete any widgets added to that layout previously
Btw. Please start new topics for new questions. It's easier for others to jump in or find it later if it's a short thread to the point, rather than finding something useful on page 13 of lengthy conversation on various topics ;) As for your question you don't define that slot quit() anywhere. You should have it in your cpp file like so
Notepad::quit() { /* do something */ }
-
Yes u're so right, the class concept destroyed all that i learned in VB, Pascal and C language... that's why i'm in trouble doing this thing.
Anyway U made my day!
Thanks, i will open a new topic for another question :D