【问题标题】:Create file using path from lineEdit from another class使用来自另一个类的 lineEdit 的路径创建文件
【发布时间】:2020-09-07 01:37:23
【问题描述】:

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

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

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

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

在“路径”行编辑中打印 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;
}

【问题讨论】:

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


    【解决方案1】:

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

    我不得不说你的代码有点乱,我认为你应该重写信号和槽的工作流程,无论如何,如果 writefile 总是在 createTxtFileName 之后调用,那么只要评论这一行就可以了

    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();
    
    }
    

    【讨论】:

    • 没错。新的“NewTxtFileName”窗口中的路径行是空的,因此没有创建文件。没看到谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多