Add widget from custom class to MainWindow
-
wrote on 4 Oct 2020, 11:24 last edited by Tamfub 10 Apr 2020, 11:50
Hi everyone.
I'm planning to create a form into
MainWindow
, and I need to add a field for an IPv4 address.
I've been looking for a smart way to handle this field and came across this solution, that seems suitable to me.In the solution, the user creates a custom class
IPCtrl
that inherits fromQFrame
and keepsQLineEdit *(m_pLineEdit[QTUTL_IP_SIZE])
i.e. a vector of 4 QLineEdits, one for each of the fields of the IP address.
I created a basic QT widget application (i.e. with
main.cpp
andmainwindow.h
/.cpp
/.ui
), and created the new class.Now, my goal is to show the QLineEdits inside my
MainWindow
. How can I do this? I tried to:-
add a private
IPCtrl
attribute in MainWindow,IPCtrl ip_field
-
set it via a setter (thus requiring the copy constructor for
IPCtrl
) -
show it via
ip_field.show()
But it results in two detached windows:
MainWindow
andip_address
.I'm up for any clarification!
-
-
Hi
The reason that ip_address becomes a window is that you dont give it a parent.
So to be inside MainWindow it needs to have that as a parent.Normally you can do like this
in .h ( in mainwindow )
IPCtrl *ip_field;in. cpp
ip_field = new IPCtrl(this);and then it should be in Mainwinow.
Do note, that normally you use a layout in MainWidnow and you insert ip_field to such layout.
-
Hi
The reason that ip_address becomes a window is that you dont give it a parent.
So to be inside MainWindow it needs to have that as a parent.Normally you can do like this
in .h ( in mainwindow )
IPCtrl *ip_field;in. cpp
ip_field = new IPCtrl(this);and then it should be in Mainwinow.
Do note, that normally you use a layout in MainWidnow and you insert ip_field to such layout.
wrote on 4 Oct 2020, 15:51 last edited by@mrjj Got it worked now, but an issue persists: now I have two
ip_field
's in myMainWindow
, and I can't figure out why.This is my code:
mainwindow.h
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); IPCtrl ip_field; private: Ui::MainWindow *ui; };
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ip_field = new IPCtrl(this); ui->formLayout->addRow("IP Address", ip_field.window()); ip_field.show(); }
Only the
ip_field
at top calls theIPCtrl
methods correctly. -
Lifetime Qt Championwrote on 4 Oct 2020, 15:55 last edited by mrjj 10 Apr 2020, 15:57
Hi
It seems that
ip_field.window()
returns something you insert into layout.So you both have the one you new (top one) and
I would guess one that ip_field.window() returns.Why are you using the .window() thing ?
what does it do ?It would not accept just
ui->formLayout->addRow("IP Address", ip_field); ?? -
Hi
It seems that
ip_field.window()
returns something you insert into layout.So you both have the one you new (top one) and
I would guess one that ip_field.window() returns.Why are you using the .window() thing ?
what does it do ?It would not accept just
ui->formLayout->addRow("IP Address", ip_field); ??wrote on 4 Oct 2020, 16:03 last edited by@mrjj No: documentation says
void QFormLayout::addRow(QWidget *label, QWidget *field) Adds a new row to the bottom of this form layout, with the given label and field.
so it takes a
QWidget
as a second parameter, and I need a way to retrieve it fromip_field
. -
@mrjj No: documentation says
void QFormLayout::addRow(QWidget *label, QWidget *field) Adds a new row to the bottom of this form layout, with the given label and field.
so it takes a
QWidget
as a second parameter, and I need a way to retrieve it fromip_field
.@Tamfub
But is IPCtrl not based on QWidget ???
like
class IPCtrl : QWidget?
-
-
@Tamfub
Hi
the first parameter needs to be a QLabel and u give it a char *
also it seems that ip_field is not pointer ? -
@Tamfub
Hi
the first parameter needs to be a QLabel and u give it a char *
also it seems that ip_field is not pointer ?wrote on 4 Oct 2020, 16:24 last edited by Tamfub 10 Apr 2020, 16:26@mrjj Actually it is ok with the
Char*
in place of theQLabel
, so I would put that apart for now.
I tried to:-
declare
IPCtrl *ip_field
(a pointer, as you suggested) inmainwindow.h
-
write
ui->formLayout->addRow("IP Address", ip_field->window()); ip_field->show();
(then with the arrows in place of the dots) inmainwindow.cpp
the application starts but the UI won't just appear...
-
-
@mrjj Actually it is ok with the
Char*
in place of theQLabel
, so I would put that apart for now.
I tried to:-
declare
IPCtrl *ip_field
(a pointer, as you suggested) inmainwindow.h
-
write
ui->formLayout->addRow("IP Address", ip_field->window()); ip_field->show();
(then with the arrows in place of the dots) inmainwindow.cpp
the application starts but the UI won't just appear...
Ok the window() part makes no sense to me.
Looking at the code, it just seems like a normal custom widget.
Did you change the code shown on SO or used it as is ? -
-
Ok the window() part makes no sense to me.
Looking at the code, it just seems like a normal custom widget.
Did you change the code shown on SO or used it as is ? -
@Tamfub
Ok i did fast test.
It just compiles so not sure why you get so much grief from it :)ui->formLayout->addRow("test", field);
test project.
https://www.dropbox.com/s/v0a5rdlkzi3igvb/IPCtrlTEst.zip?dl=0 -
@mrjj Ok, it worked also for me now, the error in
addRow
disappeares whenip_field
is declared as a pointer. I did not try this XDThank you very much!
@Tamfub
Np :)
yes it must be pointer OR you must use the & to take address but
due to how deletion works, it's best to use pointer :) -
@Tamfub
Np :)
yes it must be pointer OR you must use the & to take address but
due to how deletion works, it's best to use pointer :) -
Lifetime Qt Championwrote on 4 Oct 2020, 16:46 last edited by mrjj 10 Apr 2020, 16:47
@Tamfub
It seems the last Edit touches the QFrames border.
but even if i make it HUGE, it still happens so its
its seems to be due to
pLayout->setContentsMargins( 0, 0, 0, 0 );
so you can fix it with
pLayout->setContentsMargins( 0, 0, 1, 0 );
Seems not to give ill side effects.ps. in
IPCtrl::IPCtrl(QWidget *parent) : QFrame(parent) -
@Tamfub
It seems the last Edit touches the QFrames border.
but even if i make it HUGE, it still happens so its
its seems to be due to
pLayout->setContentsMargins( 0, 0, 0, 0 );
so you can fix it with
pLayout->setContentsMargins( 0, 0, 1, 0 );
Seems not to give ill side effects.ps. in
IPCtrl::IPCtrl(QWidget *parent) : QFrame(parent) -
@Tamfub
Np.
please mark as solved if you got it working :)
1/19