Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6342411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:14:13+00:00 2026-05-24T20:14:13+00:00

(Using Qt 4.6.3, x64, linux) I’m testing how to properly insert widgets into a

  • 0

(Using Qt 4.6.3, x64, linux)

I’m testing how to properly insert widgets into a existing a QGridLayout filled with various widgets. A broken down contrived case is the following:

QApplication app(argc,argv);
QWidget      w;
QGridLayout* gl     = new QGridLayout(&w);
QLabel*      label  = new QLabel("Image Size:");
QLineEdit*   wedit  = new QLineEdit("100");
QLabel*      xlabel = new QLabel("x");

wedit->setAlignment(Qt::AlignRight);
gl->addWidget(label);
gl->addWidget(xlabel, 0, 1, 1, 1);
gl->addWidget(wedit,  0, gl->columnCount());

Which creates the following widget:

Valid XHTML.

Assuming that have an existing QGridLayout as above, but without the “x” label, and I wished to insert this into the layout, switching the latter two addWidget lines might seem valid, i.e.:

\\ same as above
gl->addWidget(label);
gl->addWidget(wedit,  0, gl->columnCount());
gl->addWidget(xlabel, 0, 1, 1, 1);

This however, creates the following:

Valid XHTML

The gl->columnCount() after this is still 2, as both the x-label and the QLineEdit are filling the same cell. Based on this knowledge, the following code produces the initial desired result:

gl->addWidget(label);
gl->addWidget(wedit,  0, 2); // note: specified column 2, columnCount() is now 3
gl->addWidget(xlabel, 0, 1, 1, 1);

Though this is not particularly useful, as the original layout in question isn’t built with later layouts in mind.

Since addWidget allows for specifying cell position, as well as row/column span, it seems odd that Qt wouldn’t automatically replace the existing widgets in the layout. Does anyone have a suggestion as to how I might overcome this? I assume it would be possible to recreate a QGridLayout and copy into it the children of the original, taking care to insert the additional widget in the right location. This however is ugly, and susceptible to Qt version issues (as I want to modify a built in widget).

Edit:

I realize that I’m making the assumption of thinking in a QHBoxLayout way, where inserting a widget is uniquely understood, whereas in a QGridLayout this isn’t the case (?).

I can clarify that I ultimately would like to modify QFileDialog::getSaveFileName, by inserting a widget (similar to the widget shown above) right above the two lower rows (i.e. above “File &Name:”).

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:14:14+00:00Added an answer on May 24, 2026 at 8:14 pm

    A possible solution is to re-add the widgets that should be relocated. I.e.

    QLayoutItem* x01 = gl->itemAtPosition(0,1);
    gl->addWidget(x01->widget(), 0, 2);
    gl->addWidget(xlabel, 0, 1, 1, 1);
    

    Now, this isn’t particularly pretty, or easy to maintain, as a new version of Qt might change the original widget, and blindly handpicking and relocating children isn’t that clever. The following real example (the one I actually wanted to solve) was to alter the Qt’s “Save As” dialog window, that shows up using QFileDialog::getSaveFileName.

    class ImageFileDialog : public QFileDialog {
    public:
      ImageFileDialog(QWidget* parent);
      ~ImageFileDialog();
      QString getFileName() const;
      QSize getImageSize() const;
      QDialog::DialogCode exec(); // Overriden
    
    protected:
      void showEvent(QShowEvent* event); // Overriden
    
    private:
      QString    fileName_;
      QSize      imageSize_;
      QLineEdit* widthLineEdit_;
      QLineEdit* heightLineEdit_;
    };
    

    And in the source (showing just constructor, focus handling and exec):

    ImageFileDialog::ImageFileDialog(QWidget* parent)
      : fileName_(""),
        imageSize_(0,0),
        widthLineEdit_(0),
        heightLineEdit_(0)
    {
      setAcceptMode(QFileDialog::AcceptSave);
      setFileMode(QFileDialog::AnyFile);
      setConfirmOverwrite(true);
    
      QGridLayout* mainLayout = dynamic_cast<QGridLayout*>(layout());
      assert(mainLayout->columnCount() == 3);
      assert(mainLayout->rowCount()    == 4);
    
      QWidget*     container      = new QWidget();
      QGridLayout* glayout        = new QGridLayout();
      QLabel*      imageSizeLabel = new QLabel("Image Size:");
      widthLineEdit_              = new QLineEdit("400");
      heightLineEdit_             = new QLineEdit("300");
      widthLineEdit_->setAlignment(Qt::AlignRight);
      heightLineEdit_->setAlignment(Qt::AlignRight);
    
      container->setLayout(glayout);
      glayout->setAlignment(Qt::AlignLeft);
      glayout->addWidget(widthLineEdit_);
      glayout->addWidget(new QLabel("x"), 0, 1);
      glayout->addWidget(heightLineEdit_, 0, 2);
      glayout->addWidget(new QLabel("[pixels]"), 0, 3);
      glayout->addItem(new QSpacerItem(250, 0), 0, 4);
      glayout->setContentsMargins(0,0,0,0); // Removes unwanted spacing
    
      // Shifting relevant child widgets one row down.
      int rowCount = mainLayout->rowCount();
      QLayoutItem* x00 = mainLayout->itemAtPosition(mainLayout->rowCount()-2,0);
      QLayoutItem* x10 = mainLayout->itemAtPosition(mainLayout->rowCount()-1,0);
      QLayoutItem* x01 = mainLayout->itemAtPosition(mainLayout->rowCount()-2,1);
      QLayoutItem* x11 = mainLayout->itemAtPosition(mainLayout->rowCount()-1,1);
      QLayoutItem* x02 = mainLayout->itemAtPosition(mainLayout->rowCount()-1,2);
      assert(x00);   assert(x01);   assert(x10);   assert(x11);   assert(x02);
    
      mainLayout->addWidget(x00->widget(), rowCount-1, 0, 1, 1);
      mainLayout->addWidget(x10->widget(), rowCount,   0, 1, 1);
      mainLayout->addWidget(x01->widget(), rowCount-1, 1, 1, 1);
      mainLayout->addWidget(x11->widget(), rowCount,   1, 1, 1);
      mainLayout->addWidget(x02->widget(), rowCount-1, 2, 2, 1);
    
      // Adding the widgets in the now empty row.
      rowCount        = mainLayout->rowCount();
      mainLayout->addWidget(imageSizeLabel, rowCount-3, 0, 1, 1 );
      mainLayout->addWidget(container,      rowCount-3, 1, 1, 1);
    
      // Setting the proper tab-order
      QLayoutItem* tmp  = mainLayout->itemAtPosition(mainLayout->rowCount()-2,1);
      QLayoutItem* tmp2 = mainLayout->itemAtPosition(mainLayout->rowCount()-1,1);
      assert(tmp); assert(tmp2);
      QWidget::setTabOrder(heightLineEdit_      , tmp->widget());
      QWidget::setTabOrder(tmp->widget(), tmp2->widget());
    }
    
    // Makes sure the right widget is in focus
    void ImageFileDialog::showEvent(QShowEvent* event)
    {
      widthLineEdit_->setFocus(Qt::OtherFocusReason);
    }
    
    // Called to create the widget
    QDialog::DialogCode ImageFileDialog::exec()
    {
      if (QFileDialog::exec() == QDialog::Rejected)
        return QDialog::Rejected;
      // The code that processes the widget form and stores results for later calls to
      // getImageSize()
      return QDialog:Accepted;
    }
    

    Which, using for instance

    ImageFileDialog* dialog = new ImageFileDialog(&w);
    dialog->exec();
    

    Creates the following widget:

    Valid XHTML


    Comments and ways to do this better, or why this is just plain wrong are most welcome 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Windows Vista x64 with SP1, and I'm developing an ASP.NET app with
I am using VMware Workstation 6.5 on Windows Vista x64. I would like to
I am using VMware Workstation 6.5 on Windows Vista x64. I am trying to
I usually do all my java development on linux, using fedora package manager setting
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.