扩展光标长度 QLineEdit? [英] Expand cursor length QLineEdit?
本文介绍了扩展光标长度 QLineEdit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个普通的QLineEdit
,并改变光标的形状.所以有一个像这样的子类:
I'd like to take a normal QLineEdit
, and change the shape of the cursor. So with a subclass like so:
class myLineEdit : public QLineEdit
{
Q_OBJECT
signals:
public:
explicit myLineEdit(QWidget * parent = 0)
{
}
protected:
};
并使光标有几个像素宽,就像 Linux 终端一样.默认情况下,指示文本位置的光标非常纤细.
And make it so that the cursor is several pixels wide, like that of a Linux terminal. By default, the cursor to indicate text position is very slim.
我假设我需要覆盖 paintevent()
中的某些内容?paintevent
中究竟是什么负责绘制单像素闪烁线 QLineEdit()
默认为?我在文档中找不到此信息.
I assume I need to override something in the paintevent()
? What exactly in the paintevent
would be responsible for drawing the single pixel blinking line QLineEdit()
defaults to? I could not find this information in the documentation.
推荐答案
使用 Qproxystyle:
Use a Qproxystyle:
#include <QtWidgets>
class LineEditStyle: public QProxyStyle
{
Q_OBJECT
Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth)
public:
using QProxyStyle::QProxyStyle;
int cursorWidth() const{
if(m_cursor_width < 0)
return baseStyle()->pixelMetric(PM_TextCursorWidth);
return pixelMetric(PM_TextCursorWidth);
}
void setCursorWidth(int cursorWidth){
m_cursor_width = cursorWidth;
}
int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override
{
if(metric == PM_TextCursorWidth)
if(m_cursor_width > 0)
return m_cursor_width;
return QProxyStyle::pixelMetric(metric, option, widget);
}
private:
int m_cursor_width = -1;
};
class LineEdit: public QLineEdit
{
Q_OBJECT
public:
LineEdit(QWidget *parent = nullptr):
QLineEdit(parent)
{
LineEditStyle *new_style = new LineEditStyle(style());
new_style->setCursorWidth(10);
setStyle(new_style);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LineEdit w;
w.show();
return a.exec();
}
#include "main.moc"
这篇关于扩展光标长度 QLineEdit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文