6
votes

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!

2
Is there a reason you can't just check the returned QString, and pop up a second dialog with the drop-down menu if necessary? This would (probably) be more straightforward than trying to modify QFileDialog to have the extra functionality. - tmpearce
Well, if you want to do it while the file dialog is open, take a look at the QFileDialog::fileSelected signals. This will require you to not use the static function getOpenFileName - instead, create a dialog yourself and connect this signal to a slot from which you can pop up the custom dialog with drop-down menu (if that file type was selected), and it will happen each time an item is clicked. - tmpearce
Your second error is because you need to give a pointer to the combobox: addWidget(&comboBoxTemplateSelector) - tmpearce
Your first error could happen if you haven't defined the target function underneath public slots:, if you haven't included the Q_OBJECT macro in your class definition, or if you haven't run the moc tool. - tmpearce
Looks like you're mixing up value and pointer arguments, and put the & in the wrong place in the argument too. - tmpearce

2 Answers

6
votes

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.

0
votes

You can handle item selection by this signal:
void QFileDialog::fileSelected ( const QString & file )
Then it occurs, call setFilter with type you want.
Sorry, if i don't understand your task.