Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to change initial size of QLineEdit
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to change initial size of QLineEdit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 23.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nurupo
    wrote on 2 Feb 2014, 00:20 last edited by
    #1

    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;;
    

    }
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 2 Feb 2014, 10:01 last edited by
      #2

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

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

      (Z(:^

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nurupo
        wrote on 2 Feb 2014, 10:55 last edited by
        #3

        “MinimumExpanding” does nothing.

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AntonEml
          wrote on 5 Feb 2014, 17:57 last edited by
          #4

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

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nurupo
            wrote on 5 Feb 2014, 23:10 last edited by
            #5

            Nope, resize won't help.

            Got what I needed with:

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

            and then adjusting

            @
            edit.setMinimumWidth(n);
            @

            if needed.

            1 Reply Last reply
            0

            1/5

            2 Feb 2014, 00:20

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved