Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying QFileDialog::getOpenFileName to have an additional drop down

I am a student programmer using Qt to build a reader Table for my company. This reader is both an editor and converter. It reads in a .i file allows table editing of a text document and then puts out a .scf file which is essentially a separated value file stacked under a legend built with headers. I digress... Basically the file format imported is really hard to scan and read in(mostly impossible) so what I'd like to is modify the open file preBuilt QFileDialog to include an additional drop down when older file types are selected to declare their template headers.

When the user selects .i extension files(option 2 file type) I would like to enable an additional drop down menu to allow the user to select which type of .i file it is(template selected). This way I don't have to deal with god knows how many hours trying to figure out a way to index all the headers into the table for each different type. Currently my importFile function calls the dialog using this:

QString fileLocation = QFileDialog::getOpenFileName(this,("Open File"), "", ("Simulation Configuration File(*.scf);;Input Files(*.prp *.sze *.i *.I *.tab *.inp *.tbl)")); //launches File Selector

I have been referencing QFileDialog Documentation to try and find a solution to what I need but have had no avail. Thanks for reading my post and thanks in advance for any direction you can give on this.

UPDATE MAR 16 2012; First I'd like to give thanks to Masci for his initial support in this matter. Below is the connect statement that I have along with the error I receive.

//Declared data type
    QFileDialog openFile;
    QComboBox comboBoxTemplateSelector;
    connect(openFile, SIGNAL(currentChanged(const &QString)), this, SLOT(checkTemplateSelected()));
    openFile.layout()->addWidget(comboBoxTemplateSelector);

compile errors

I also noticed that it didn't like the way I added the QComboBox to the modified dialog's layout(which is the second error). I really hope that I'm just doing something dumb here and its an easy task to overcome.

In response to tmpearce's comment heres my header code;

#include <QWidget>
namespace Ui {
class ReaderTable;
}
class ReaderTable : public QWidget
{
    Q_OBJECT
public:
    explicit ReaderTable(QWidget *parent = 0);
    ~ReaderTable();
public slots:
    void checkTemplateSelected();
    void importFile();
    void saveFile();
private:
    Ui::ReaderTable *ui;
};

Thanks for reading and thanks in advance for any contributions to this challenge!

like image 793
Wylie Coyote SG. Avatar asked Mar 14 '12 17:03

Wylie Coyote SG.


1 Answers

Instance a QFileDialog (do not call getOpenFileName static method), access its layout and add a disabled QComboBox to it.

// mydialog_ and cb_ could be private fields inside MyClass
mydialog_ = new QFileDialog;
cb_ = new QComboBox;
cb_->setEnabled(false);
connect(mydialog, SIGNAL(currentChanged(const QString&)), this, SLOT(checkFilter(const QString&)));
mydialog_->layout()->addWidget(cb_);

if (mydialog_->exec() == QDialog::Accepted) {
    QString selectedFile = mydialog_->selectedFiles()[0];
    QString cbSelection = cb_->currentText();
}

the slot would be something like:

void MyClass::checkFilter(const QString& filter) 
{
  cb_->setEnabled(filter == "what_you_want");
}

returning from the dialog exec(), you could retrieve selected file and cb_ current selection. Notice you could add something more complex than a simple QComboBox at the bottom of the dialog, taking care of gui cosmetics.

Actually I don't like very much this approach (but that was what you asked for :-). I would make a simple dialog like this:

enter image description here

and enable the combo only if the selected file meets your criteria. The "browse" button could call getOpenFileMethod static method in QFileDialog.

like image 138
Masci Avatar answered Nov 19 '22 13:11

Masci