英文:
How to display vector elements on my Qt application interface?
问题
我有两个函数getProjects()
和getEmployees()
,在一个叫做manage.h
的类中,它们基本上返回一个包含我迄今为止添加的所有项目/员工的向量。
如何在我的Qt应用程序界面中显示它?
我创建了两个按钮(在stackedWidget
中),一个用于在单击时获取所有项目,另一个用于员工,我还使用了一个QListView
,当我单击其中一个按钮时应该显示所有我的数据,但如何告诉QListView
显示我的向量元素呢?
我认为解决方案可能类似于这样:
void MainWindow::on_showAllProjects_pushButton_clicked()
{
ui->listView->something(manage.getProjects());
}
英文:
I have two functions getProjects()
and getEmployees()
, in a class called manage
from manage.h
, that basically return a vector with all the projects/employees I've added until now.
How can I display it in my Qt application interface?
I've created two buttons (in a stackedWidget
), one to get all the projects when clicked and the other one for the employees, and I've also used a QListView
that should show all my data when I click one of the two buttons, but how do I tell the QListView to display my vector's elements?
I think the solution would look like something like this:
void MainWindow::on_showAllProjects_pushButton_clicked()
{
ui->listView->something(manage.getProjects());
}
答案1
得分: 0
你首先需要一个QStringList来存放向量的元素,然后是一个QStringListModel,你需要将其stringlist设置为你的stringlist,然后将这个QStringListModel设置为你的listview模型,就可以实现你需要的功能。
这是一个例子:
QStringListModel *model = new QStringListModel(this);
QStringList List;
List << "Project1" << "Project2" << "Project3";
model->setStringList(List);
ui->listView->setModel(model);
如果你不必使用基于模型的列表,可以使用QListWidget,它提供了一个addItem()
方法,与QListView相比更直观。
如果你需要更多细节或深度,你可能会发现这个链接有些帮助:
https://www.bogotobogo.com/Qt/Qt5_QListView_QStringListModel_ModelView_MVC.php
英文:
You first need a QStringList to put the elements of your vector into, then a QStringListModel, which you need to set its stringlist to your stringlist, then set this QStringListModel as your listview model, and that does what you need.
Here's an example:
QStringListModel *model = new QStringListModel(this);
QStringList List;
List << "Project1" << "Project2" << "Project3";
model->setStringList(List);
ui->listView->setModel(model);
If you don't have to use the model based list, you can use QListWidget instead, it provides a method addItem()
which is very straightforward compared to QListView.
if you need more details or depth, you may find this link of some use:
https://www.bogotobogo.com/Qt/Qt5_QListView_QStringListModel_ModelView_MVC.php
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论