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
QtWS25 Last Chance

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

    jsulmJ 1 Reply Last reply
    0
    • E Offline
      E Offline
      Eligijus
      wrote on 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

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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 last edited by mtrch
          #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
          3
          • M mtrch

            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 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.sueM Offline
              m.sueM Offline
              m.sue
              wrote on last edited by m.sue
              #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
              3
              • m.sueM m.sue

                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 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.sueM Offline
                  m.sueM Offline
                  m.sue
                  wrote on last edited by m.sue
                  #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

                  • Login

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