使用来自另一个类的lineEdit的路径创建文件

时间:2020-05-20 15:46:50

标签: c++ qt dialog signals-slots qlineedit

在我的项目中,我有一个MainWindow.cpp文件,其中包含“新建Txt文件”按钮。该按钮将打开NewTxtFileName对话框,在其中可以通过按“创建”按钮来创建新的text.txt文件。

然后将打开一个保存对话框,我可以选择目录和新文件的名称。完成后,文件的完整路径将显示在“ Path” lineEdit内,可用于打开第二个对话框。第二个对话框是WriteFileDialog,我可以在其中添加作者的姓名,然后单击“确定”来写入文件。

enter image description here

似乎MainWindow类没有使用lineEdit中的文件路径来调用WriteFileDialog

如果我将TxtFile类中的NewTxtFileName::getpath()变量替换为QString TxtFile= QDir::homePath+QDir::separator()+"untitled.txt",则第二个对话框打开,将文件和内容写入新位置。

重要的是要在“ Path” lineEdit中打印pe的文件路径。如何正确使用所需的文件位置和名称打开WriteFileDialog并将其保存?

MainWindow.cpp

void MainWindow::createTxtFileName()
{
    newtxtfilename = new NewTxtFileName(this);
    connect(newtxtfilename,SIGNAL(ready()),this,SLOT(writefile()));
    newtxtfilename->show();
    newtxtfilename->activateWindow();
}


void MainWindow::writefile()
{
    newfilename = new NewTxtFileName(this);
    QString fileName = newfilename->getpath();

    if (fileName.isEmpty()) return;

    newwritedialog = new WriteFileDialog(this,fileName);
    connect(newwritedialog,SIGNAL(opensignal()),this,SLOT(openfile()));
    newwritedialog->show();
    newwritedialog->activateWindow();

}

NewTxtFileName.cpp

void NewTxtFileName::on_CreateFileButton_clicked()
{
    QString filePathName = QFileDialog::getSaveFileName(this, tr("File Name"),
            QDir::homePath()+QDir::separator()+"untitled.txt","txt Files (*.txt)");

    ui->Path->setText(filePathName);
    if (filePathName.isEmpty()) return;
}

QString NewTxtFileName::getpath()
{
    QString TxtFile= ui->Path->text();
    //QString TxtFile= QDir::homePath+QDir::separator()+"untitled.txt"
    QString path = TxtFile;
    return path;
}

void NewTxtFileName::on_OkButton_accepted() //15/5
{
    ready();
    close();
}

WriteFileDialog.cpp

WriteFileDialog::WriteFileDialog(QWidget *parent,QString fileName) :
    QDialog(parent),
    ui(new Ui::WriteFileDialog)
{
    ui->setupUi(this);
    connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(create()));

    txtfileName = fileName;
    setWindowTitle(QFileInfo(fileName).fileName());

}

void WriteFileDialog::create() {

    QString fileText = "Author Name : ";
    QString fileText += ui->AuthorlineEdit->text();

    QFile file(txtfileName);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    return;
    QTextStream textStream(&file);
    textStream.flush();
    textStream << fileText;
    file.close();

    emit opensignal(txtfileName);

}

WriteFileDialog::~WriteFileDialog()
{
    delete ui;
}

1 个答案:

答案 0 :(得分:1)

那是因为您正在创建 NewTxtFileName 的新实例,而没有设置路径。

我不得不说您的代码有点混乱,我认为您应该重写信号和插槽工作流程,无论如何,如果始终在 createTxtFileName之后调用 writefile ,则只需注释此行即可

void MainWindow::writefile()
{
    //You are creating a new instance of the NewTxtFileName window where you never set the path
    //newfilename = new NewTxtFileName(this);
    QString fileName = newfilename->getpath();

    //also try using QDebug so that you know what fileName contains
    qDebug() << fileName;
    if (fileName.isEmpty()) return;

    newwritedialog = new WriteFileDialog(this,fileName);
    connect(newwritedialog,SIGNAL(opensignal()),this,SLOT(openfile()));
    newwritedialog->show();
    newwritedialog->activateWindow();

}