如何关联 Combobox 的选中项写入文件

Posted

技术标签:

【中文标题】如何关联 Combobox 的选中项写入文件【英文标题】:How to associate the selected item of Combobox to write into a file 【发布时间】:2015-05-12 17:37:51 【问题描述】:

基本上我有一个line-edit 框,它接受用户输入(例如逗号分隔值)并单击push-button 它将行编辑框的所有值写入文本文件。但我想通过combobox 实现相同的功能。因此,每当从combobox 中选择一项时,它应该将line-edit 框的内容写入文本文件。

到目前为止我使用按钮实现的代码

void MainWindow::writefile()

    QString str = ui->lineEdit->text();
    QString filename = "data.txt";
    QFile file(filename);
    if(file.open(QIODevice::WriteOnly|QIODevice::Text))
    
    QTextStream out(&file);
    out<<str<<endl;
    file.close();
    

void MainWindow::on_pushButton_clicked()

    writefile();
 

【问题讨论】:

【参考方案1】:

听起来您需要做的就是实现 ItemListener 并使 itemStateChanged 完全按照您的 actionPerformed 所做的工作。

【讨论】:

以上是关于如何关联 Combobox 的选中项写入文件的主要内容,如果未能解决你的问题,请参考以下文章

MFC中,怎么获取ComboBox的选中项

win10系统中安全选项里没有创建文件/写入数据,该如何添加?

将文本行写入R中的文件

innosetup如何往注册表写入多条

WinForm中ComboBox添加Key/Value项获取选中项根据KeyValue设置选中项总结

将文本行写入R中的文件