简体   繁体   English

如何在没有QLabel的Qt中添加超链接?

[英]How to add hyperlinks in Qt without QLabel?

I have some labels and layouts nested inside a QWidget to build a part of a sidebar. 我在QWidget中嵌套了一些标签和布局,以构建侧边栏的一部分。 Each QWidget is its own section and one component currently looks like this: 每个QWidget是其自己的部分,并且一个组件当前如下所示:

gui的屏幕截图

To my understanding, you can only set hyperlinks with QLabel, but I'm trying to get the whole area between the white lines clickable. 据我了解,您只能使用QLabel设置超链接,但是我试图使白线之间的整个区域都可单击。 This is including the icon and the whitespace. 这包括图标和空格。 Is there any way to achieve this? 有什么办法可以做到这一点?

This got marked as a duplicate to the opposite of what I was asking, so I'd like to reiterate that I'm trying to implement a hyperlink without QLabel. 这被标记为与我要问的相反的重复项,因此我想重申一下,我正在尝试在没有 QLabel的情况下实现超链接。

You can easily have a widget open a link on click: 您可以轻松地使小部件在单击时打开链接:

class Link : public QWidget {
  Q_OBJECT
  public:
  Link(QUrl url, QWidget p = nullptr) : QWidget(p), _url(url) {}
  QUrl _url;
  void mouseReleaseEvent(QMouseEvent *) { QDesktopServices::openUrl(_url); }
}

You can avoid any extra signals and connections, and have each link widget store its own link internally, the url can be set on construction and changed at any time. 您可以避免任何额外的信号和连接,并让每个链接窗口小部件在内部存储自己的链接,可以在构造时设置url并随时更改。 Not using signals and slots makes it easier to change the link too, without having to disconnect previous connections. 不使用信号和插槽,也可以更轻松地更改链接,而不必断开先前的连接。

IMO going for a signals and slots solution is only justified when you want different arbitrary behavior. IMO寻求信号和插槽解决方案仅在您想要不同的任意行为时才是合理的。 In this case you always want the same - to open a particular link, so you might as well hardcode that and go for an easier and more computationally efficient solution. 在这种情况下,您总是想要相同的-打开特定的链接,因此您也可以对其进行硬编码,并寻求一种更简单,计算效率更高的解决方案。

I would just manually catch the SIGNAL for clicked() and use desktop services to open the url in code. 我只是手动捕获clicked()的信号并使用桌面服务在代码中打开URL。

bool QDesktopServices::openUrl ( const QUrl & url ) [static]

Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; 在适用于用户桌面环境的Web浏览器中打开给定的url,如果成功,则返回true;否则,返回true。 otherwise returns false. 否则返回false。

http://doc.qt.io/qt-4.8/signalsandslots.html http://doc.qt.io/qt-4.8/signalsandslots.html

Using this type of syntax, or in the designer, you can also connect a signal to a slot. 使用这种语法或在设计器中,您还可以将信号连接到插槽。

connect(widgetThatRepresentsURL, SIGNAL(clicked()),
    handlerThatWillOpenTheURL, SLOT(clicked_on_url()));

For widgets that don't have a signal set up for clicked (or whatever event you are interested in), you can subclass the widget in question and reimplement... 对于没有为单击(或您感兴趣的任何事件)设置信号的窗口小部件,可以将有问题的窗口小部件子类化并重新实现...

void QWidget::mousePressEvent ( QMouseEvent * event )   [virtual protected]

Specifically for creating a signal, there is emit . 专门用于产生信号的是emit I've used this in the past like the following 我过去像下面这样使用

void Cell::focusInEvent(QFocusEvent *e)
{
  emit focus(this, true);
  QLineEdit::focusInEvent(e);
}

with the following in the header 标头中包含以下内容

signals:
  void focus(Cell *, bool);

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

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