【发布时间】:2022-01-10 01:24:20
【问题描述】:
对于自定义小部件,有些选项卡禁止使用ALT + <C> 快捷方式访问,其中<C> 可以是任何键盘字符键。在 Qt 中,这称为Mnemonic
对于这个快捷方式,需要在标签中将该字母加下划线。
我可以看到QPainter::drawText 有一个标志参数,可以用Qt::TextShowMnemonic 提供,但我想在使用QStaticText 时使用它以提高性能。 QStaticText 允许富文本,但似乎不支持下划线,或者我无法使其工作。
#include <QApplication>
#include <QDebug>
#include <QStaticText>
#include <QPainter>
#include <QPaintEvent>
#include <QWidget>
class TestWidget: public QWidget
{
Q_OBJECT
public:
explicit TestWidget( QWidget* parent=nullptr):QWidget(parent){}
auto paintEvent(QPaintEvent *event) -> void override
{
QPainter p(this);
QStaticText staticText; // this is not how it should be used, but for the example...
staticText.setTextFormat(Qt::TextFormat::RichText);
staticText.setText("<u>F</u>ile"); //What happens with Underline?
p.drawStaticText(QPoint(50,50), staticText);
p.drawText(QRect(50, 80, 100, 100), Qt::TextShowMnemonic, "&File"); // Ok, this works, but no static-text
}
};
#include "main.moc"
auto main (int argn, char* args[])-> int
{
QApplication app(argn, args);
qDebug() << QT_VERSION_STR;
TestWidget w;
w.resize(200,200);
w.show();
return app.exec();
}
结果:
问题是:
如何使用QStaticText制作下划线或&助记符?
.
【问题讨论】:
-
staticText.setText("<u>F</u>ile");是真的渲染标签还是只是忽略它们? -
只是忽略了它们。 (我添加了一张图片)
-
这个好像有QT-BUG,快10岁了。
-
谢谢@SebastianDietrich,我现在明白了。我想除了使用
drawText函数之外别无他法。 -
注意:如果您想将此作为答案,我会接受/赞成。 (我知道反正不是什么大奖励)