Hi i want get the text string of QListView in my main window when i click a button in Qdialog. my implementation is :
in Qdialog
void hist::getValue(){
QModelIndexList templatelist =
ui->listView->selectionModel()->selectedIndexes();
QStringList stringlist;
foreach (const QModelIndex &index, templatelist) {
stringlist.append(index.data(Qt::DisplayRole).toString());
}
qDebug()<<stringlist;
// return stringlist; // what i need to here to return stringlist ?
}
void hist::on_downloadselected_clicked() {
connect(ui->downloadselected, SIGNAL(clicked()), SLOT(accept()));
// TODO selected download
}
in mainwindow
void mainwindow::on_pushButton_2_clicked()
{
hist history;
history.exec();
if( history.exec() == QDialog::Accepted ){
QString damn = history.getValue(); // am getting error here
ui->url->setText(damn);
qDebug()<<"pressed";
}
}
Your compilation error is of course because your method returned void
, and you tried to use its return value anyway. You probably want something like this, untested code:
// changed to return first selection, or empty QString if no selection
QString hist::getValue(){
QModelIndexList templatelist =
ui->listView->selectionModel()->selectedIndexes();
if (templatelist.isEmpty())
return QString(); // empty string
else
return templatelist.first().data(Qt::DisplayRole).toString();t ?
}
void hist::on_downloadselected_clicked() {
// why did you even have connect here?
if (!getValue().isEmpty())
accept(); // close the dialog with accept if selection made
else
reject(); // or do nothing?
}
Solved my problem with the folowing codes in particular file in mainwindow
void mainwindow::on_pushButton_2_clicked()
{
hist history;
// history.exec();
if( history.exec() == QDialog::Accepted){
ui->url->setText(history.inputstring());
on_downloadButton_clicked();
}
else if( history.close() == QDialog::Rejected ) {
history.close();
}
}
In dialog
QString hist::inputstring() {
QModelIndexList templatelist =
ui->listView->selectionModel()->selectedIndexes();
QString stringlist;
foreach (const QModelIndex &index, templatelist) {
stringlist.append(index.data(Qt::DisplayRole).toString());
}
return stringlist;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.