![](/img/trans.png)
[英]How to change the text of a QLineEdit when the user click on the QLineEdit in Qt?
[英]how to change the text of qlineedit when Qlistview index is changed in another qdialog?
嗨,當我單擊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
,並且您仍然嘗試使用其返回值。 您可能需要類似未經測試的代碼:
// 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?
}
解決了主窗口中特定文件中以下代碼的問題
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.