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. Can I increase the max width of QLineEdit when setting its alignment in a layout?
Forum Updated to NodeBB v4.3 + New Features

Can I increase the max width of QLineEdit when setting its alignment in a layout?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.8k Views 2 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.
  • D Offline
    D Offline
    DragonautX
    wrote on last edited by DragonautX
    #1

    I was testing around with layouts after trying the "Using Layouts" tutorial. I aligned a QLineEdit to Qt::AlignRight in a QHboxLayout. I noticed the width shrunk to 100 pixels (I checked with std::cout) compared to when I didn't edit its alignment. I wanted to make it wider. After seeing QlineEdit inherits QWidget, and seeing that QWidget has setMaximumWidth(), I tried using that with 200 pixels, but it didn't work. Can I increase the maximum width of a QLineEdit even if I align it in a layout?

    #include <QtWidgets>
    #include <iostream>
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
        QWidget window;
        window.resize(320, 340);
        window.show();
        window.setToolTip("This is the Top-level widget");
        window.setWindowTitle(QApplication::translate("toplevel", "Top-level widget"));
    
        QLineEdit edit("Line Edit", &window);
        edit.setMaximumWidth(200); //didn't make 'edit' wider
    
        QHBoxLayout layout;
        layout.addWidget(&edit);
        layout.setAlignment(&edit, Qt::AlignRight);
    
        window.setLayout(&layout);
    
        std::cout << edit.width();
    
        return app.exec();
    
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Maximum width is the maximum size the widget can get, not the one it will have. If you want to ensure the size it at least 200 you should call setMinimumWidth.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        DragonautX
        wrote on last edited by
        #3

        That makes sense. Thanks!

        1 Reply Last reply
        0

        • Login

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