如何在QMap中存储数据& QT中的Qlist和Reterivethem?
我的要求是我有星期一至星期日的时间表名称和“星期日,12:00-17:30”的时间表项目.........
现在我想拥有2个QlistWidget,一个包含计划名称,其他包含计划项目,现在当我在第一个列表框中选择一个计划名称时,相应的计划项目会列在另一个列表框中。
如何使用Qmap和Qlist向listwidget添加项目? 如何使用它保持数据内存?
QList<QString> ScheduleItemslist;
ScheduleItemslist << "Saturday,09:00- 17:30" << "Sunday,10:00- 12:00" << "Sunday,01:30- 17:30" ;
QList<QString> ScheduleItemslst;
ScheduleItemslst << "Monday,09:00- 17:30" << "Tuesday,10:00- 12:00" << "Wednesday,01:30- 17:30" ;
QMap<QString,QList<QString> > map;
map["Weekend Plus"] = ScheduleItemslist ;
map["All"] = ScheduleItemslst ;
foreach (QString key, map.keys())
{
ui->listWidgetTimeSet->addItem (key) ;
}
ui->listWidgetTime->addItems(ScheduleItemslist);
在ItemselectionChanged事件中:
QList<QString> ScheduleItemslist;
ScheduleItemslist << "Saturday,09:00- 17:30" << "Sunday,10:00- 12:00" << "Sunday,01:30- 17:30" ;
QList<QString> ScheduleItemslst;
ScheduleItemslst << "Monday,09:00- 17:30" << "Tuesday,10:00- 12:00" << "Wednesday,01:30- 17:30" ;
if(ui->listWidgetTimeSet->currentItem()->text() == "Weekend Plus")
{
ui->listWidgetTime->addItems(ScheduleItemslist);
}
else if (ui->listWidgetTimeSet->currentItem()->text() == "All")
{
ui->listWidgetTime->addItems(ScheduleItemslst);
}
答案 0 :(得分:3)
您需要这样的东西来存储您的数据。
//Declare your map
QMap <QString, QList <QString> > map;
//Populate your map in a function
void populateMap ()
{
QList<QString> sundaylist;
sundaylist<< "Sunday , 12:00 - 17:30" << "Sunday , 18:00 - 20:30";
map ["Sunday"] = sundaylist;
QList<QString> mondaylist;
mondaylist<< "Monday , 12:00 - 17:30" << "Monday , 18:00 - 20:30";
map ["Monday"] = mondaylist
}
//Populate list1 with keys in your map
void populateList1 ()
{
list1.addItems (map.keys ());
}
//This is your slot connected to currentItemChanged signal of list1
void list1CurrentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous )
{
list2.clear ();
QString key = current->text ();
list2.addItems (map [key]);
}