【问题标题】:how to change the text of qlineedit when Qlistview index is changed in another qdialog?如何更改QlistView索引在另一个QDialog中更改时QlineEdit的文本?
【发布时间】:2015-11-25 16:09:42
【问题描述】:

嗨,当我单击 Qdialog 中的按钮时,我想在主窗口中获取 QListView 的文本字符串。我的实现是:

在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
}

在主窗口中

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";
    }
}

【问题讨论】:

  • 更新原型 void hist::getValue() 以在主窗口中返回 Qstring,如果您需要 Qlist 中的所有值(在主窗口和 qdialog 中),则返回 QStringList
  • @Megasa3 什么论据??
  • @Megasa3 当从包含 Qlistview 的对话框中单击按钮时,我想在我的主窗口中获取所选列表项的文本。
  • "am getting error here",通常最好知道是什么错误。虽然这里很清楚,但您的方法返回void,即什么也没有。如果选择了多个项目,你想做什么?也许您应该防止选择多个项目(通过设置列表小部件的适当属性)?然后只需让getValue 以 QString 的形式返回第一个(希望是唯一的)选择。
  • 我知道昨晚会把我的解决方案放在这里

标签: c++ qt c++11 qt4


【解决方案1】:

你的编译错误当然是因为你的方法返回了void,而你还是试图使用它的返回值。您可能想要这样的未经测试的代码:

// 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?
}

【讨论】:

  • 我昨晚解决了我的问题,感谢代码我做了类似上面的事情:)
【解决方案2】:

解决了我在特定文件中使用以下代码的问题 在主窗口中

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

在对话框中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    相关资源
    最近更新 更多