6

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!

Wylie Coyote SG.
  • 1,009
  • 6
  • 22
  • 37
  • 1
    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 Mar 14 '12 at 17:57
  • I completely agree; I also feel that that would be more guiding for our customers as they might not think to check the file types that are allowed to be displayed. However, I find that my company would like to make accessing older file types a little difficult to really get them extinct as fast as possible. All opinions aside; we have never had a standard with these files(which is why selected templates are so necessary) and this new extension is an attempt at that. – Wylie Coyote SG. Mar 14 '12 at 18:02
  • 1
    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 Mar 14 '12 at 18:11
  • I see the QFileDialog::fileSelected signals but do you have any idea how to add anything to the QFileDialog? – Wylie Coyote SG. Mar 14 '12 at 18:19
  • What I'm suggesting is to *not* modify QFileDialog, rather, implement a simple dialog that *pops up when needed*. If you need to have that functionality embedded inside the QFileDialog itself, I can't help you other than to say you'll probably have to derive a subclass. – tmpearce Mar 14 '12 at 18:24
  • 2
    Your second error is because you need to give a pointer to the combobox: `addWidget(&comboBoxTemplateSelector)` – tmpearce Mar 16 '12 at 15:34
  • 1
    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 Mar 16 '12 at 15:36
  • I am not familiar with the Q_OBJECT macro but it is included in my class definitions. I have updated my post in with my header code – Wylie Coyote SG. Mar 16 '12 at 16:00
  • Change `connect(openFile, SIGNAL(currentChanged(const &QString))...` to `connect(&openFile, SIGNAL(currentChanged(const QString&))...` – tmpearce Mar 16 '12 at 20:41
  • 1
    Looks like you're mixing up value and pointer arguments, and put the `&` in the wrong place in the argument too. – tmpearce Mar 16 '12 at 20:42
  • Thanks! you were right! I was missing that &reference in openFile In addition it looks as though the added comboBox still wont show up.. Everything seems appropriate. ahhhh:>/ Shouldn't it appear in my dialog using openFile.layout()->addWidget(&comboBoxTemplateSelector); – Wylie Coyote SG. Mar 16 '12 at 20:51

2 Answers2

6

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.

Community
  • 1
  • 1
Masci
  • 5,864
  • 1
  • 26
  • 21
  • I like this solution; whether it works depends on if the layout handles the extra combobox (or full widget) in a pleasing way, and how to enforce that the user actually select one of the options. That's why I suggested subclassing, but this could perhaps be made to work. – tmpearce Mar 14 '12 at 18:29
  • @tmpearce agree. I gave it a try with PyQt with a simple QLabel appended to the dialog, I think some work will be needed to align things, and in any case "Cancel" and "Open" buttons would remain above the added widget. – Masci Mar 14 '12 at 18:44
  • Im having problems with the connect statement; I understand the concept but im not sure I'm calling on the right things. Another curious event is that the SIGNAL field doesn't populate any type of intellisense which makes me wonder how signals are passed here. I tried: connect(openFile, SIGNAL(openFile.filterSelected("Input Files(*.prp *.sze *.i *.I *.tab *.inp *.tbl)")), this, SLOT(checkTemplateSelected())); and connect(openFile, SIGNAL(currentChanged(const &QString)), this, SLOT(checkTemplateSelected())); Any advice here? – Wylie Coyote SG. Mar 14 '12 at 19:08
  • The correct connection is: connect(openFile, SIGNAL(currentChanged(const &QString)), this, SLOT(checkTemplateSelected())); What's the error? – Masci Mar 14 '12 at 21:38
0

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.

Raxillan
  • 233
  • 2
  • 6