在我的Qt应用界面上显示矢量元素如何实现?

huangapple go评论68阅读模式
英文:

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 &lt;&lt; &quot;Project1&quot; &lt;&lt; &quot;Project2&quot; &lt;&lt; &quot;Project3&quot;;

model-&gt;setStringList(List);
ui-&gt;listView-&gt;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

huangapple
  • 本文由 发表于 2023年2月19日 08:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定