Add widget from custom class to MainWindow
-
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.
-
@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(); }
mainwindow.ui
This is the result:
Only the
ip_field
at top calls theIPCtrl
methods correctly. -
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); ?? -
@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 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...
-
-
@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 -
@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)