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. Adding pushButton on QFileDialog
Forum Update on Monday, May 27th 2025

Adding pushButton on QFileDialog

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 4.0k Views
  • 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.
  • S Offline
    S Offline
    samdol
    wrote on 9 Jan 2017, 06:42 last edited by
    #1

    Hi
    I tried to add a pushButton on QFileDialog by

    class FileDialog : public QFileDialog
    {
        QPushButton *pb;
    public:
        FileDialog(QWidget *parent) : QFileDialog(parent){}
    
        void addWidget()
        {
            QGridLayout* mainLayout = dynamic_cast <QGridLayout*>(this->layout());
            if( !mainLayout){
                qDebug()<<"mainLayout is NULL";
            }else{
    
                QHBoxLayout *hbl =new QHBoxLayout(0);
                pb =new QPushButton(QString("My checkbox"));
                hbl->addWidget(pb);
                int num_rows = mainLayout->rowCount();
                qDebug()<<"num_rows: "<<num_rows;
                mainLayout->addLayout(hbl, num_rows, 0, 1, -1);
            }
        }
    };
    
    When I executed
    
        FileDialog *fd=new FileDialog(this);
        fd->addWidget();
    

    I got the result,
    "mainLayout is NULL"
    It means I could not get the QGridLayout* by this->layout().
    Is it a bug? I use Qt 5.6.2 in windows7.

    J 1 Reply Last reply 9 Jan 2017, 10:01
    0
    • E Offline
      E Offline
      Eligijus
      wrote on 9 Jan 2017, 07:18 last edited by
      #2

      Try to use http://doc.qt.io/qt-5/qobject.html#qobject_cast . Or just pass layout pointer as argument in your function.

      1 Reply Last reply
      0
      • S samdol
        9 Jan 2017, 06:42

        Hi
        I tried to add a pushButton on QFileDialog by

        class FileDialog : public QFileDialog
        {
            QPushButton *pb;
        public:
            FileDialog(QWidget *parent) : QFileDialog(parent){}
        
            void addWidget()
            {
                QGridLayout* mainLayout = dynamic_cast <QGridLayout*>(this->layout());
                if( !mainLayout){
                    qDebug()<<"mainLayout is NULL";
                }else{
        
                    QHBoxLayout *hbl =new QHBoxLayout(0);
                    pb =new QPushButton(QString("My checkbox"));
                    hbl->addWidget(pb);
                    int num_rows = mainLayout->rowCount();
                    qDebug()<<"num_rows: "<<num_rows;
                    mainLayout->addLayout(hbl, num_rows, 0, 1, -1);
                }
            }
        };
        
        When I executed
        
            FileDialog *fd=new FileDialog(this);
            fd->addWidget();
        

        I got the result,
        "mainLayout is NULL"
        It means I could not get the QGridLayout* by this->layout().
        Is it a bug? I use Qt 5.6.2 in windows7.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 9 Jan 2017, 10:01 last edited by
        #3

        @samdol I guess the layout in a QFileDialog is not QGridLayout, that's why the cast fails.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mtrch
          wrote on 9 Jan 2017, 11:23 last edited by mtrch 1 Sept 2017, 11:24
          #4

          This is because QFileDialog uses native file dialog by default, you just need to turn off this behavior:

          FileDialog(QWidget *parent) : QFileDialog(parent)
          {
              setOption(QFileDialog::DontUseNativeDialog);
          }
          
          S 1 Reply Last reply 10 Jan 2017, 09:26
          3
          • M mtrch
            9 Jan 2017, 11:23

            This is because QFileDialog uses native file dialog by default, you just need to turn off this behavior:

            FileDialog(QWidget *parent) : QFileDialog(parent)
            {
                setOption(QFileDialog::DontUseNativeDialog);
            }
            
            S Offline
            S Offline
            samdol
            wrote on 10 Jan 2017, 09:26 last edited by
            #5

            @mtrch
            Thank you mtrch, That makes it return QGridLayout correctly,
            but
            fileName = fd->getOpenFileName(this,
            tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
            does not show any change on FileDialog.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              m.sue
              wrote on 10 Jan 2017, 09:35 last edited by m.sue 1 Oct 2017, 09:52
              #6

              getOpenFileName is a static function. It does not care about any changes you made to fd.

              You will have to do everything of the static function yourself:

              fd->setWindowTitle("Open Image");
              fd->setAcceptMode(QFileDialog::AcceptOpen);
              fd->setDirectory("/home/jana");
              fd->setFileMode(QFileDialog::ExistingFile);
              fd->setViewMode(QFileDialog::Detail);
              fd->setNameFilter(tr("Image Files (*.png *.jpg *.bmp)"));
              if (!fd->exec())
              	//cancelled
              	return;
              fileName=fd->selectedFiles().first();
              

              -Michael.

              S 1 Reply Last reply 10 Jan 2017, 12:19
              3
              • M m.sue
                10 Jan 2017, 09:35

                getOpenFileName is a static function. It does not care about any changes you made to fd.

                You will have to do everything of the static function yourself:

                fd->setWindowTitle("Open Image");
                fd->setAcceptMode(QFileDialog::AcceptOpen);
                fd->setDirectory("/home/jana");
                fd->setFileMode(QFileDialog::ExistingFile);
                fd->setViewMode(QFileDialog::Detail);
                fd->setNameFilter(tr("Image Files (*.png *.jpg *.bmp)"));
                if (!fd->exec())
                	//cancelled
                	return;
                fileName=fd->selectedFiles().first();
                

                -Michael.

                S Offline
                S Offline
                samdol
                wrote on 10 Jan 2017, 12:19 last edited by
                #7

                @m.sue
                Thanks. It works as expected. Can I set the style of fd to the style of the QFileDialog on executing getOpenFileName()?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  m.sue
                  wrote on 10 Jan 2017, 13:03 last edited by m.sue 1 Oct 2017, 13:03
                  #8

                  "style": IIRC the static function uses the native dialog, by default. If you mean that, then no (see above).

                  1 Reply Last reply
                  1

                  2/8

                  9 Jan 2017, 07:18

                  topic:navigator.unread, 6
                  • Login

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