[SOLVED] Arranging Items with fixed sizes
This is probably a question that has been asked multiple times, but I've searched on the forums/web/documentation and can't find out how to make this work.
I have a basic Qt application and I want my LineEdit to remain a certain size
int main(int argc, char* argv)
QApplication app(argc, argv);
QMainWindow *win = new QMainWindow(); QWidget *centralWidget = new QWidget(); QDockWidget *dock = new QDockWidget(); QLineEdit *txtIP = new QLineEdit(); QBoxLayout *lay = new QBoxLayout(QBoxLayout::LeftToRight,centralWidget); int x,y; const int height = QApplication::desktop()->width(); const int width = QApplication::desktop()->height(); /* Line Edit */ txtIP->setGeometry(20,20,190,25); lay->addWidget(txtIP); /* Central Widget */ //centralWidget->setLayout(lay); win->setCentralWidget(centralWidget); /* Main App window setup */ x=((int)width/2)-200; y=((int)height/2)-300; win->setGeometry(y,x,400,600); win->show(); return app.exec();
I would like something like this:
but I end up with:
It's like it completely ignores that I want my LineEdit 190x25 at x:20,y:20 and auto scales the LineEdit to take up the maximum available width
The problem is that you're using the QBoxLayout which manages your QLineEdit position. If you like to use absolute positioning you should add the QLineEdit directly to central widget.
MainWindow::MainWindow(QWidget *parent) :
QWidget *centralWidget = new QWidget();
QLineEdit *txtIP = new QLineEdit( centralWidget ); txtIP->setGeometry(50,50,190,25); resize( 500, 300 );
Note that you've created dock widget just in memory but visually is "nowhere" (no parent, not shown) so it won't be visible.
Thank you! I know I've created a dock item, I was planning on using it once I understood why the sizes were being ignored.