c++ - 将变量传递给其他对话框 Qt

标签 c++ qt qtreeview qtgui qdialog

我有一个带有文本文件列表的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/

相关文章:

c++ - 链表 C++ 中的复制构造函数

qt - Qt中的OpenGL深度测试不起作用

multithreading - Qt向不同线程发送信号

python - 自定义委托(delegate)项目编辑器小部件出现在错误的位置

c++ - QTreeWidgetItem 层次结构到 SQL

c++ - qtreeview 的 qt eventfilter 不起作用?

c++ - 无法将 QMap 传递给 SLOT

c++ - 分类 imagenet - caffe/caffe.hpp : No such a file or directory

c++ - printf 调用搞砸了 std::thread 但 std::cout 很好

c++ - Qt 的 WCF 服务?