C++ QT C++;已选择QListWidget的索引
如何获取QListWidget的选定索引。我可以获得选定的颜色,但现在确定如何获得项目的选定索引。 我已经编写了选定的颜色函数。请帮助我获取颜色的选定索引C++ QT C++;已选择QListWidget的索引,c++,qt,C++,Qt,如何获取QListWidget的选定索引。我可以获得选定的颜色,但现在确定如何获得项目的选定索引。 我已经编写了选定的颜色函数。请帮助我获取颜色的选定索引 ColorList::ColorList(QWidget *parent) : QListWidget(parent) { init(); } QString ColorList::selectedColor() const { return currentItem() ? currentItem()->dat
ColorList::ColorList(QWidget *parent)
: QListWidget(parent)
{
init();
}
QString ColorList::selectedColor() const
{
return currentItem() ? currentItem()->data(Qt::UserRole).toString() : QString();
}
void ColorList::init()
{
setFrameShape(QFrame::NoFrame);
QMap<QString, QString> names;
names["Air"] = "#FFFFFF";
names["Resist"] ="#B22222";
names["BARC"] = "#F2CBC5";
names["Oxide"] = "#34AAD1";
names["Low"] = "#FD7E00";
// add color names and their icons
foreach(const QString &key, names.keys())
{
QPixmap px(16,16);
px.fill(QColor(names[key]));
QListWidgetItem *item = new QListWidgetItem(QIcon(px), key);
item->setData(Qt::UserRole, names[key]);
addItem(item);
}
}
ColorList::ColorList(QWidget*parent)
:QListWidget(父级)
{
init();
}
QString ColorList::selectedColor()常量
{
返回currentItem()?currentItem()->数据(Qt::UserRole).toString():QString();
}
void ColorList::init()
{
setFrameShape(QFrame::NoFrame);
QMap名称;
姓名[“Air”]=“#FFFFFF”;
名称[“Resist”]=“#B22222”;
名称[“BARC”]=“F2CBC5”;
名称[“氧化物”]=“#34AAD1”;
名称[“低”]=“#FD7E00”;
//添加颜色名称及其图标
foreach(常量QString&key,names.keys())
{
QPixmap-px(16,16);
px.fill(QColor(名称[键]);
QListWidgetItem*item=新的QListWidgetItem(QIcon(px),键);
item->setData(Qt::UserRole,name[key]);
增编(项目);
}
}
是currentRow()
。有关文档,请参见此处