XXDebugger
XXDebugger

Reputation: 1581

QT C++ SelectedIndex of QListWidget

How can I get the selected index of 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);
    }
}

Upvotes: 2

Views: 4778

Answers (1)

Wassim Gr
Wassim Gr

Reputation: 588

It's currentRow(). Please see here for the documentation http://doc.qt.io/qt-4.8/qlistwidget.html#currentRow-prop

Upvotes: 3

Related Questions