在 Qt 中如何在 QLineEdit 中添加带有 QFileDialog 的文件
Posted
技术标签:
【中文标题】在 Qt 中如何在 QLineEdit 中添加带有 QFileDialog 的文件【英文标题】:In Qt how to add a file with a QFileDialog in a QLineEdit 【发布时间】:2016-10-19 14:43:36 【问题描述】:我有一个窗口,它有一个名为“...”的按钮,它打开另一个窗口,该窗口也有一个名为“...”的按钮,它打开一个 QFileDialog。在第二个窗口的按钮旁边,有10个QLineEdit来获取10个文件。使用 QFileDialog,我无法获取 mp3 文件并将他的名字放在第一个 QLineEdit 中。我能怎么做 ?如果第一个 QLineEdit 已经被占用了,我如何将一个 mp3 文件的名称放在第二个 QLineEdit 中?
文件“main.h”只是 Qt 的包含。这是我的代码,我尝试了许多解决方案但没有成功:
#include "main.h"
int main(int argc, char **argv)
QApplication program(argc, argv);
/* ******************** First Window ******************** */
QWidget window;
QVBoxLayout layout;
window.setWindowTitle("Programme");
window.setWindowIcon(QIcon(":/Images/Images/Bouclier.png"));
window.setFixedSize(450, 400);
QHBoxLayout layouttop;
QLabel labeltop("Nom du programme :");
QLineEdit lineedittop;
layouttop.addWidget(&labeltop);
layouttop.addWidget(&lineedittop);
QHBoxLayout layoutmiddle;
QPushButton buttonnext;
QPushButton buttonprevious;
QPushButton buttonetc("...");
QLineEdit line0;
QLineEdit line1;
QLineEdit line2;
QLineEdit line3;
QLineEdit line4;
QLineEdit line5;
QLineEdit line6;
QLineEdit line7;
QLineEdit line8;
QLineEdit line9;
QVBoxLayout layoutleft;
layoutleft.addWidget(&buttonnext);
buttonnext.setIcon(QIcon(":/Images/Images/Haut.png"));
layoutleft.addWidget(&buttonprevious);
buttonprevious.setIcon(QIcon(":/Images/Images/Bas.png"));
QVBoxLayout layoutcenter;
layoutcenter.addWidget(&line0);
layoutcenter.addWidget(&line1);
layoutcenter.addWidget(&line2);
layoutcenter.addWidget(&line3);
layoutcenter.addWidget(&line4);
layoutcenter.addWidget(&line5);
layoutcenter.addWidget(&line6);
layoutcenter.addWidget(&line7);
layoutcenter.addWidget(&line8);
layoutcenter.addWidget(&line9);
line0.setReadOnly(true);
line1.setReadOnly(true);
line2.setReadOnly(true);
line3.setReadOnly(true);
line4.setReadOnly(true);
line5.setReadOnly(true);
line6.setReadOnly(true);
line7.setReadOnly(true);
line8.setReadOnly(true);
line9.setReadOnly(true);
QVBoxLayout layoutright;
layoutright.addWidget(&buttonetc);
layoutmiddle.addLayout(&layoutleft);
layoutmiddle.addLayout(&layoutcenter);
layoutmiddle.addLayout(&layoutright);
QHBoxLayout layoutbottom;
QPushButton buttonapply("Apply");
QPushButton buttoncancel("Cancel");
layoutbottom.addWidget(&buttonapply);
layoutbottom.addWidget(&buttoncancel);
layout.addLayout(&layouttop);
layout.addLayout(&layoutmiddle);
layout.addLayout(&layoutbottom);
window.setLayout(&layout);
window.show();
/* ******************** First Window ******************** */
/* ******************** Second Window ******************** */
QWidget windowed;
QVBoxLayout layouted;
windowed.setWindowTitle("Session");
windowed.setWindowIcon(QIcon(":/Images/Images/Bouclier.png"));
windowed.setFixedSize(450, 400);
QHBoxLayout layouttoped;
QLabel labeltoped("Nom de la session :");
QLineEdit lineedittoped;
layouttoped.addWidget(&labeltoped);
layouttoped.addWidget(&lineedittoped);
QHBoxLayout layoutmiddled;
QPushButton buttonnexted;
QPushButton buttonprevioused;
QPushButton buttonetced("...");
QString file;
QLineEdit lined0;
QLineEdit lined1;
QLineEdit lined2;
QLineEdit lined3;
QLineEdit lined4;
QLineEdit lined5;
QLineEdit lined6;
QLineEdit lined7;
QLineEdit lined8;
QLineEdit lined9;
QComboBox box;
QLabel label("Durée :");
QVBoxLayout layoutlefted;
layoutlefted.addWidget(&buttonnexted);
buttonnexted.setIcon(QIcon(":/Images/Images/Haut.png"));
layoutlefted.addWidget(&buttonprevioused);
buttonprevioused.setIcon(QIcon(":/Images/Images/Bas.png"));
QVBoxLayout layoutcentered;
layoutcentered.addWidget(&lined0);
layoutcentered.addWidget(&lined1);
layoutcentered.addWidget(&lined2);
layoutcentered.addWidget(&lined3);
layoutcentered.addWidget(&lined4);
layoutcentered.addWidget(&lined5);
layoutcentered.addWidget(&lined6);
layoutcentered.addWidget(&lined7);
layoutcentered.addWidget(&lined8);
layoutcentered.addWidget(&lined9);
lined0.setReadOnly(true);
lined1.setReadOnly(true);
lined2.setReadOnly(true);
lined3.setReadOnly(true);
lined4.setReadOnly(true);
lined5.setReadOnly(true);
lined6.setReadOnly(true);
lined7.setReadOnly(true);
lined8.setReadOnly(true);
lined9.setReadOnly(true);
QVBoxLayout layoutrighted;
layoutrighted.addWidget(&buttonetced);
QVBoxLayout layoutmore;
QHBoxLayout layoutmored;
layoutmored.addWidget(&label);
layoutmored.addWidget(&box);
box.addItem("30");
box.addItem("60");
layoutmore.addLayout(&layoutmored);
layoutmiddled.addLayout(&layoutlefted);
layoutmiddled.addLayout(&layoutcentered);
layoutmiddled.addLayout(&layoutrighted);
layoutmiddled.addLayout(&layoutmore);
QHBoxLayout layoutbottomed;
QPushButton buttonapplyed("Apply");
QPushButton buttoncanceled("Cancel");
layoutbottomed.addWidget(&buttonapplyed);
layoutbottomed.addWidget(&buttoncanceled);
layouted.addLayout(&layouttoped);
layouted.addLayout(&layoutmiddled);
layouted.addLayout(&layoutbottomed);
windowed.setLayout(&layouted);
/* ******************** Second Window ******************** */
/* ******************** Actions First Window ******************** */
QObject::connect(&buttonetc, &QPushButton::clicked, &windowed, &QWidget::show);
/* ******************** Actions First Window ******************** */
/* ******************** Actions Second Window ******************** */
QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &file] file = QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (*.mp3)")););
lined0.setText(Qstring(file));
/* ******************** Actions Second Window ******************** */
return program.exec();
【问题讨论】:
你已经得到了返回的文件名。只需将其设置为行编辑的文本。并且您可以在此之前检查此文本是否为空。 @Hayt,我在 QFileDialog 下方添加了lined0.setText(QString(file));
和 lined0 是第一个 QLineEdit 但没有附加任何内容。
【参考方案1】:
也许你可以尝试在一个窗口中发出文件名的地址,然后通过槽函数在第二个窗口中接收这个地址
【讨论】:
或者你可以把文件名命名为全局变量,QFileDialog可以打开一个路径,然后把这个路径赋值给文件名,QLineEdit也使用这个全局变量以上是关于在 Qt 中如何在 QLineEdit 中添加带有 QFileDialog 的文件的主要内容,如果未能解决你的问题,请参考以下文章
如何使 QPushButtons 将文本添加到 QLineEdit 框中?
QT中,在QLineEdit控件中输入一个数值,比如0.2,如何获取这个数值来使用呢?
Qt - 如何将 QListView 项目转移到另一个 QListView?