Solved how to attach class to qt project without ui file?
-
created a class which added to the project without ui.
I can't call it correctly.** the class I'm embedding: **
#ifndef OKNONASTROEK_H #define OKNONASTROEK_H #include <QMainWindow> #include <QDebug> #include <QPainter> #include <QPen> #include <QFont> #include <QFontMetrics> #include <QKeyEvent> #include <QRegion> #include <QPointF> #include <QGraphicsView> namespace Widgets { struct texnStructur { texnStructur () // immediately fill it with zeros { for (uint i = 0; i <sizeof (m_t_razm) / sizeof (m_t_razm [0]); i ++) m_t_razm [i] = 0; } float m_t_razm [12]; }; class window pane: public QWidget { Q_OBJECT public: windowspar (QWidget * parent = 0); ~ windowspawn (); void razmer_element (int x, int y); protected: void paintEvent (QPaintEvent * event); texnStructur _myTexpr; }; } #endif // OKNONASTROEK_H
** here is the "body" of the class **
#include "oknonastroek.h" Widgets :: windowastroek :: windowastroek (QWidget * parent) : QWidget (parent) { } Widgets :: windowspawn :: ~ windowpattern () { } void Widgets :: oknonastroek :: razmer_element (int x, int y) { // set the size of the lines here _myTexpr.m_t_razm [0] = x / 60.0; // X indent _myTexpr.m_t_razm [1] = g / 4.0 + g / 60.0; // Y indent _myTexpr.m_t_razm [2] = x / 3.0; // width (window) x _myTexpr.m_t_razm [3] = g / 1.4; // height (window) y } void Widgets :: windowastroek :: paintEvent (QPaintEvent * event) { QPainter painter (this); // Create a renderer object QBrush bgfon (QColor (180,220,220), Qt :: SolidPattern); // set the background color and fill type (with background without background and fill type) painter.setPen (Qt :: NoPen); // paint brush if not needed painter.setRenderHint (QPainter :: Antialiasing); // remove sharp cubes painter.setBrush (bgfon); // set the background color razmer_element (this-> width (), this-> height ()); // set the size of the element painter.drawRect (_myTexpr.m_t_razm [0], _myTexpr.m_t_razm [1], _myTexpr.m_t_razm [2], _myTexpr.m_t_razm [3]); }
** And here's the main class: **
#ifndef OKNOWIBORA_H #define OKNOWIBORA_H #include <QMainWindow> #include <QDebug> #include <QPainter> #include <QPen> #include <QFont> #include <QFontMetrics> #include <QKeyEvent> #include <QRegion> #include <QPointF> #include <QGraphicsView> #include "oknonastroek.h" struct texnStructur { texnStructur () // immediately fill it with zeros { for (uint i = 0; i <sizeof (m_t_razm) / sizeof (m_t_razm [0]); i ++) m_t_razm [i] = 0; } float m_t_razm [12]; }; WindowVibora class: public QMainWindow { Q_OBJECT public: WindowWibora (QWidget * parent = 0); ~ WindowVibora (); void razmer_element (int x, int y); protected: void paintEvent (QPaintEvent * event); Widgets :: windowstrip * oknonastr; texnStructur _myTexpr; }; #endif // OKNOWIBORA_H
** "body" of the main class **
#include "oknowibora.h" OknoWibora :: OknoWibora (QWidget * parent) : QMainWindow (parent) { oknonastr = new Widgets :: oknonastroek (this); } OknoWibora :: ~ OknoWibora () { } void OknoWibora :: paintEvent (QPaintEvent * event) { QPainter painter (this); // Create a renderer object QBrush bgfon (QColor (220,180,220), Qt :: SolidPattern); // set the background color and fill type (with background without background and fill type) QPen pen_abris (Qt :: black, 2, Qt :: SolidLine, Qt :: FlatCap); // outline (compass) brush QPen pen_line (Qt :: black, 2, Qt :: SolidLine, Qt :: FlatCap); // brush for lines and text painter.setRenderHint (QPainter :: Antialiasing); // remove sharp cubes painter.setBrush (bgfon); // set the background color painter.setPen (Qt :: NoPen); // paint brush if not needed razmer_element (this-> width (), this-> height ()); // set the dimensions painter.drawRect (_myTexpr.m_t_razm [0], _myTexpr.m_t_razm [1], _myTexpr.m_t_razm [2], _myTexpr.m_t_razm [3]); oknonastr-> update (); qDebug () << "rere1"; } void OknoWibora :: razmer_element (int x, int y) { // set the size of the lines here _myTexpr.m_t_razm [0] = x / 60.0; // X indent _myTexpr.m_t_razm [1] = y / 60.0; // Y indent _myTexpr.m_t_razm [2] = x- (x / 60.0) * 2.0; // width (window) x _myTexpr.m_t_razm [3] = y / 4.0; // height (window) y qDebug () << "x:" << x << "_myTexpr.m_t_razm [0]:" << _myTexpr.m_t_razm [0]; qDebug () << "x:" << x << "_myTexpr.m_t_razm [1]:" << _myTexpr.m_t_razm [1]; qDebug () << "y:" << y << "_myTexpr.m_t_razm [2]:" << _myTexpr.m_t_razm [2]; qDebug () << "y:" << y << "_myTexpr.m_t_razm [3]:" << _myTexpr.m_t_razm [3]; }
here is the announcement
oknonastr = new Widgets :: oknonastroek (this);
here is an update
oknonastr-> update ();
if there was ui, then the file attached to the project, then the turquoise square would be beautifully displayed and so straight it is sad
-
OK, I explain in details:
When you don't use an UI from the Designer, you need to:
- set the central widget
- add a layout to the central widget
- add others widgets to the layout
QWidget* centralWidget= new QWidget; setCentralWidget(centralWidget); QBoxLayout* layout= new QVBoxLayout; centralWidget->setLayout(layout); layout->addWidget( *yourTurquoiseWidget*);
-
I think you need to put your 'turquoise widget' in a layout.
-
@mpergand said in how to attach class to qt project without ui file?:
turquoise widget
OknoWibora::OknoWibora(QWidget *parent)
: QMainWindow(parent)
{
display = new QLineEdit();QGridLayout *mainLayout = new QGridLayout; oknonastr = new Widgets::OknoNastroek(this); mainLayout->addWidget(oknonastr); mainLayout->addWidget(display);
}
-
@timob256 said in how to attach class to qt project without ui file?:
windowspar (QWidget * parent = 0);
~ windowspawn ();Typo or does your class c'tor have a different name than your d'tor?
-
@Pl45m4 ????? I don't have such variables c'tor and d'tor
-
@timob256 c'tor - constructor, d'tor - destructor.
Not vars but methods and yes, you have them. -
@timob256 said in how to attach class to qt project without ui file?:
QGridLayout *mainLayout = new QGridLayout;
oknonastr = new Widgets::OknoNastroek(this);
mainLayout->addWidget(oknonastr);
mainLayout->addWidget(display);You're missing something ;)
Find a perfect example for you:
https://github.com/HBPVIS/Servus/blob/master/apps/servusBrowser.cpp -
OknoWibora::OknoWibora(QWidget *parent)
: QMainWindow(parent)
{
// QGridLayout *mainLayout = new QGridLayout;
// oknonastr = new Widgets::OknoNastroek(this);
// mainLayout->addWidget(oknonastr);
// mainLayout->addWidget(display);QGridLayout *mainLayout = new QGridLayout; oknonastr = new Widgets::OknoNastroek(this); mainLayout->addWidget(oknonastr);
}
the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(
-
OK, I explain in details:
When you don't use an UI from the Designer, you need to:
- set the central widget
- add a layout to the central widget
- add others widgets to the layout
QWidget* centralWidget= new QWidget; setCentralWidget(centralWidget); QBoxLayout* layout= new QVBoxLayout; centralWidget->setLayout(layout); layout->addWidget( *yourTurquoiseWidget*);
-
-
@timob256 said in how to attach class to qt project without ui file?:
OknoWibora::OknoWibora(QWidget *parent)
: QMainWindow(parent)
{
// QGridLayout *mainLayout = new QGridLayout;
// oknonastr = new Widgets::OknoNastroek(this);
// mainLayout->addWidget(oknonastr);
// mainLayout->addWidget(display);QGridLayout *mainLayout = new QGridLayout; oknonastr = new Widgets::OknoNastroek(this); mainLayout->addWidget(oknonastr);
}
the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(
this work
thsanks mpergand -
@timob256 said in how to attach class to qt project without ui file?:
mainLayout
You did not add this layout to any widget or other layout. Don't know how this can work...