9

在(或,等)中显示可点击URL的最佳方式是什么?QTableViewQTreeViewQListView

鉴于QStandardItemModel某些列包含带有 URL 的文本,我希望它们变得可点击,然后通过使用处理点击QDesktopServices::openURL()

我希望有一些简单的方法来利用QLabel's textInteraction 标志并将它们塞进表格中。我不敢相信没有更简单的方法来处理这个问题。我真的希望我错过了什么。

4

3 回答 3

7

您需要创建一个委托来进行绘画。代码应如下所示:

void RenderLinkDelegate::paint(
           QPainter *painter,
           const QStyleOptionViewItem &option,
           const QModelIndex &index
           ) const
{
    QString text = index.data(Qt::DisplayRole).toString();
    if (text.isEmpty())
        return;

    painter->save();

    // I only wanted it for mouse over, but you'll probably want to remove
    // this condition
    if (option.state & QStyle::State_MouseOver)
    {
        QFont font = option.font;
        font.setUnderline(true);
        painter->setFont(font);
        painter->setPen(option.palette.link().color());
    }
    painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, text);

    painter->restore();
}
于 2010-03-03T23:42:56.970 回答
4

好吧,您可以使用委托在 qtableview 中呈现富文本,并使用自定义委托重新实现绘制方法,例如:

void CHtmlDelegate::paint(QPainter *painter,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index) const
{
    QStyleOptionViewItemV4 opt(option);

    QLabel *label = new QLabel;
    label->setText(index.data().toString());
    label->setTextFormat(Qt::RichText);
    label->setGeometry(option.rect);
    label->setStyleSheet("QLabel { background-color : transparent; }");

    painter->translate(option.rect.topLeft());
    label->render(painter);
    painter->translate(-option.rect.topLeft());
}

但是,它不会使超链接可点击。

为此,您可以使用以下技巧。重新实现表/列表视图的 setModel 方法并使用 setIndexWidget。

void MyView::setModel(QAbstractItemModel *m)
{
  if (!m)
    return;

  QTableView::setModel(m);

  const int rows = model()->rowCount();
  for (int i = 0; i < rows; ++i)
    {
      QModelIndex idx = model()->index(i, 1);

      QLabel *label = new QLabel;
      label->setTextFormat(Qt::RichText);
      label->setText(model()->data(idx, CTableModel::HtmlRole).toString());
      label->setOpenExternalLinks(true);

      setIndexWidget(idx, label);
    }
}

在上面的示例中,我将第 1 列替换为 qlabels。请注意,您需要取消模型中的显示角色以避免重叠数据。

无论如何,我会对基于代表的更好解决方案感兴趣。

于 2013-12-13T12:50:03.153 回答
2

可悲的是,使用 a (而不是使用 a QLabel)来渲染 a 并不容易。没有神奇的两行代码可以调用并完成工作。setOpenExternalLinks()QTableViewQTableWidget

  1. 使用委托
  2. 将委托设置为表格的列
  3. QTextDocument结合使用来setHTML()呈现一个 html 链接
  4. 这意味着您的模型需要提供一个 HTML 片段,其中包含a href
  5. 计算链接的几何形状并提供事件处理程序以拦截鼠标
    • 当光标位于链接上时更改光标
    • 单击链接时执行操作
  6. 什么乱七八糟:(我想要painter->setWidgetToCell()

--

void LabelColumnItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{

    if( option.state & QStyle::State_Selected )
        painter->fillRect( option.rect, option.palette.highlight() );


    painter->save();

    QTextDocument document;                         // <---- RichText 
    document.setTextWidth(option.rect.width());

    QVariant value = index.data(Qt::DisplayRole);
    if (value.isValid() && !value.isNull())
    {
        document.setHtml(value.toString());        // <---- make sure model contains html

        painter->translate(option.rect.topLeft());
        document.drawContents(painter);        
    }

    painter->restore();
}
于 2016-03-08T13:28:02.983 回答