Beispiel #1
0
void tst_QMenu::widgetActionFocus()
{
    //test if the focus is correctly handled with a QWidgetAction
    QMenu m;
    QListWidget *l = new QListWidget(&m);
    for (int i = 1; i<3 ; i++)
        l->addItem(QString("item%1").arg(i));
    QWidgetAction *wa = new QWidgetAction(&m);
    wa->setDefaultWidget(l);
    m.addAction(wa);
    m.setActiveAction(wa);
    l->setFocus(); //to ensure it has primarily the focus
    QAction *menuitem1=m.addAction("menuitem1");
    QAction *menuitem2=m.addAction("menuitem2");

    m.popup(QPoint());

    QVERIFY(m.isVisible());
    QVERIFY(l->hasFocus());
    QVERIFY(l->currentItem());
    QCOMPARE(l->currentItem()->text(), QString("item1"));

    QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
    QVERIFY(l->currentItem());
    QCOMPARE(l->currentItem()->text(), QString("item2"));

    QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
    QVERIFY(m.hasFocus());
    QCOMPARE(m.activeAction(), menuitem1);

    QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
    QVERIFY(m.hasFocus());
    QCOMPARE(m.activeAction(), menuitem2);

    QTest::keyClick(QApplication::focusWidget(), Qt::Key_Up);
    QVERIFY(m.hasFocus());
    QCOMPARE(m.activeAction(), menuitem1);

    QTest::keyClick(QApplication::focusWidget(), Qt::Key_Up);
    QVERIFY(l->hasFocus());
    QCOMPARE(m.activeAction(), (QAction *)wa);
}
Beispiel #2
0
void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QListWidget *eventlist = (QListWidget*) this->parent();
    bool isFocusedList = true;
    if (eventlist)
        isFocusedList = eventlist->hasFocus();

    bool isSelectedItem = option.state & QStyle::State_Selected;
    QString text = index.data(Qt::DisplayRole).toString();
    text = getHtmlWithColours(text, isSelectedItem, isFocusedList);
    QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
    QSize iconSize = icon.actualSize(QSize(option.rect.size().height() * 2, option.rect.size().height()));
    QRect iconRect = QRect(option.rect.x() + EVENT_ITEM_ICON_MARGIN, option.rect.y() + EVENT_ITEM_ICON_MARGIN, iconSize.width(), iconSize.height());
    QPixmap iconPixmap = icon.pixmap(iconSize);
    QPoint textTopLeft = QPoint(iconRect.x() + iconSize.width() + EVENT_ITEM_ICON_MARGIN + EVENT_ITEM_TEXT_MARGIN, option.rect.y() + EVENT_ITEM_ICON_MARGIN + EVENT_ITEM_TEXT_MARGIN);
    QSize textSize = QSize(option.rect.width() - textTopLeft.x(), option.rect.height() - textTopLeft.y());
    QRect textRect = QRect(textTopLeft, textSize);
    QTextDocument document;
    document.setUndoRedoEnabled(false);

    if (isSelectedItem)
    {
        const QPalette::ColorRole color = (isFocusedList) ? QPalette::Highlight : QPalette::Window;
        painter->resetTransform();
        painter->fillRect(option.rect, option.palette.color(color));
    }

    //painter->save();
    //painter->restore();
    painter->resetTransform();

    document.setHtml(text);
    painter->drawPixmap(iconRect, iconPixmap);

    //painter->save();
    //painter->restore();
    painter->resetTransform();
    painter->translate(textRect.topLeft());
    painter->setRenderHint(QPainter::Antialiasing, true);

    document.drawContents(painter);

    //painter->save();
    //painter->restore();
}