gpt4 book ai didi

c++ - 在 Qt 中如何在 QLineEdit 中使用 QFileDialog 添加文件

转载 作者:行者123 更新时间:2023-11-30 16:55:55 24 4
gpt4 key购买 nike

我有一个窗口,其中有一个名为“...”的按钮,它打开另一个窗口,该窗口还有一个名为“...”的按钮,它打开一个 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();
}

最佳答案

也许你可以尝试在一个窗口中发出文件名的地址,然后通过槽函数在第二个窗口中接收该地址

关于c++ - 在 Qt 中如何在 QLineEdit 中使用 QFileDialog 添加文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40134680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com