[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
@
#include <iostream>
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QTGui>
#include <QDesktopWidget>
#include <QDockWidget>
#include <QBoxLayout>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:
!http://www.img.elwillow.net/pics/bac3dbbbd6d3dc02ce22eda4f530e9f4.jpg(Desired Output)!but I end up with:
!http://www.img.elwillow.net/pics/2aa03c0c63ab4407469aa22168790dc9.jpg(Current Output)!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
-
Hi,
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.
Working example:
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget *centralWidget = new QWidget();
setCentralWidget(centralWidget);QLineEdit *txtIP = new QLineEdit( centralWidget ); txtIP->setGeometry(50,50,190,25); resize( 500, 300 );
}
MainWindow::~MainWindow()
{
}
@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.