Important: Please read the Qt Code of Conduct -

[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 */
    /* Central Widget */
    /* Main App window setup */
    return app.exec&#40;&#41;;


    I would like something like this:
    ! Output)!

    but I end up with:
    ! 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) :
    QWidget *centralWidget = new QWidget();

    QLineEdit *txtIP = new QLineEdit( centralWidget );
    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.

Log in to reply