我有一个带有文本文件列表的QTreeView
。如果选择了一个文件并且 void FileList_dialog::on_openButton_released()
它应该将变量 path
传递给对话框 textFile_dialog
。
到目前为止,我有这个:
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();
}
但是如何将变量 path
传递给 textFile_dialog
?
最佳答案
你有几个选择:
1) 将路径传递给对话框构造函数
代码看起来像这样:
文本文件_对话框.h
class TextFile_Dialog : public QDialog
{
Q_OBJECT
public:
explicit TextFile_Dialog(const QString &path, QObject *parent = 0);
...
private:
QString m_path;
};
文本文件_对话框.cpp
...
#include "textfile_dialog.h"
...
TextFile_Dialog::TextFileDialog(const QString &path, QObject *parent)
: QDialog(parent)
, m_path(path)
{
...
}
...
然后你会像这样使用这个类:
textFile_dialog textFile_Dialog(path);
2) 您还可以使用 setter 方法来设置路径,如下所示:
文本文件_对话框.h
class TextFile_Dialog : public QDialog
{
Q_OBJECT
public:
...
void setPath(const QString &path);
...
private:
QString m_path;
};
文本文件_对话框.cpp
...
#include "textfile_dialog.h"
...
void TextFile_Dialog::setPath(const QString &path)
{
m_path = path;
}
...
那么用法是这样的:
textFile_dialog textFile_Dialog;
textFile_Dialog.setPath(path);
关于c++ - 将变量传递给其他对话框 Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20948665/