I have a QTreeView
with a list of text files. If a file is selected and void FileList_dialog::on_openButton_released()
it should pass a variable path
to dialog textFile_dialog
.
Until now I've this:
void FileList::on_openButton_released()
{
QModelIndex index = ui->treeView->currentIndex();
QFileSystemModel *model = (QFileSystemModel*)ui->treeView->model();
QString path = model->filePath(index);
QString name = model->fileName(index);
QString dir = path;
QFile file(path);
qDebug() << path;
textFile_dialog textFile;
textFile.setModal(true);
textFile.exec();
}
But how do I pass the variable path
to textFile_dialog
?
You have have several options:
1) Pass the path to the dialog constructor
The code would look something like this:
textfile_dialog.h
class TextFile_Dialog : public QDialog
{
Q_OBJECT
public:
explicit TextFile_Dialog(const QString &path, QObject *parent = 0);
...
private:
QString m_path;
};
textfile_dialog.cpp
...
#include "textfile_dialog.h"
...
TextFile_Dialog::TextFileDialog(const QString &path, QObject *parent)
: QDialog(parent)
, m_path(path)
{
...
}
...
Then you would use the class like this:
textFile_dialog textFile_Dialog(path);
2) You could also have a setter method for setting the path as follows:
textfile_dialog.h
class TextFile_Dialog : public QDialog
{
Q_OBJECT
public:
...
void setPath(const QString &path);
...
private:
QString m_path;
};
textfile_dialog.cpp
...
#include "textfile_dialog.h"
...
void TextFile_Dialog::setPath(const QString &path)
{
m_path = path;
}
...
Then the usage would be like this:
textFile_dialog textFile_Dialog;
textFile_Dialog.setPath(path);
In the header of the textFile_dialog class, you add a parameter to the constructor:
explicit textFile_dialog(const QString &a_path);
and in the FileList::on_openButton_released() change the constructor call to:
textFile_dialog textFile(path);