简体   繁体   English

QT C ++的QT C ++ SelectedIndex

[英]QT C++ SelectedIndex of QListWidget

How can I get the selected index of QListWidget. 如何获取QListWidget的选定索引。 I can get the selected color but now sure how can I get the selected Index of the item. 我可以获得选定的颜色,但是现在确定如何获得选定的项目索引。 I have written the selected color function. 我已经编写了选定的颜色功能。 Please help me in getting the selected index of the color. 请帮助我获取选定的颜色索引。

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);
    }
}

It's currentRow() . 这是currentRow() Please see here for the documentation http://doc.qt.io/qt-4.8/qlistwidget.html#currentRow-prop 请参阅此处获取文档http://doc.qt.io/qt-4.8/qlistwidget.html#currentRow-prop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM