[Solved] How to change initial size of QLineEdit



  • I have QMainWindow <- QDockWidget <- QVBoxLayout <- QLineEdit. The dock widget is vertical, i.e. on either left or right sides of the main window.

    The problem is that the initial width of QLineEdit is too big, it stretches QDockWidget too much.
    I want QLineEdit to have smaller initial width.

    I can't set fixed or maximum sizes on the QLineEdit, because I want QLineEdit to stretch when user resizes QDockWidget.

    So far only subclassing QLineEdit and overriding sizeHint() to return a smaller value got me what I wanted. The thing is that I already have 4 different widgets subclassed just to override sizeHint(). There should be a better way of doing this.

    @
    #include <QApplication>
    #include <QMainWindow>
    #include <QDockWidget>
    #include <QVBoxLayout>
    #include <QLineEdit>
    #include <QWidget>
    #include <QTextEdit>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QMainWindow w;

    QDockWidget dock(&w);
    QWidget dummy(&dock);
    QVBoxLayout layout(&dummy);
    
    QLineEdit edit(&dummy);
    edit.setText("Text");
    //make edit even longer
    

    #if QT_VERSION >= 0x050200
    edit.setClearButtonEnabled(true);
    #endif
    // edit.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); <- that's what fixes it
    layout.addWidget(&edit);

    dummy.setLayout(&layout);
    dock.setWidget(&dummy);
    w.addDockWidget(Qt::LeftDockWidgetArea, &dock);
    
    // just to take some space
    QTextEdit randomWidget(&w);
    w.setCentralWidget(&randomWidget);
    
    w.show();
    
    return a.exec(&#41;;
    

    }
    @


  • Moderators

    What happens when you set the size policy to "MinimumExpanding"?

    Also, make sure the minimum size for that line edit is sufficiently small.



  • “MinimumExpanding” does nothing.

    I want to change edit's width to something smaller, without dirty subclassing of line edit and then overriding of sizeHint().



  • may be you should use resize() function to set initial size



  • Nope, resize won't help.

    Got what I needed with:

    @
    edit.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    @

    and then adjusting

    @
    edit.setMinimumWidth(n);
    @

    if needed.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.