3

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:

};

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.

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.

AAEM
  • 1,837
  • 2
  • 18
  • 26
progro
  • 33
  • 4

1 Answers1

1

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"

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I had actually tried something similar but it was for Qt4.x and I'm using Qt5 but I'm going to give this a try and report back, thanks. – progro Feb 28 '19 at 19:43
  • @progro QProxyStyle only existed in Qt4 since 4.6 (latest versions), but in Qt4 it is already fully available. Are you using Qt5? – eyllanesc Feb 28 '19 at 19:46
  • I'm using Qt5.9 and this doesn't compile - error: undefined reference to `vtable for LineEdit' , any ideas? I tried clean project & run qmake, and also tried to remove the Q_OBJECT macro, none of that worked.ANy ideas? – progro Feb 28 '19 at 19:47
  • @progro If you are using my code only in the main.cpp then you must add main.moc: `"#include "main.moc"` at the end of the file, execute `build -> run qmake` and compile again. and if you are copying it to another file then delete main.moc – eyllanesc Feb 28 '19 at 19:49
  • @progro some feedback? – eyllanesc Feb 28 '19 at 19:52
  • Yep, that works! Thanks! If you're looking for work I'd like to keep your information on file, please send me your email if available (I couldn't find it on your paypal page). – progro Feb 28 '19 at 19:55
  • @progro see my github: https://github.com/eyllanesc there is my contact information – eyllanesc Feb 28 '19 at 20:01
  • Thanks! Just a FYI - I played with the code a bit and since nothing is actually being animated the Q_OBJECT & Q_PROPERTY calls aren't necessary, so if you get rid of the "using QProxyStyle::QProxyStyle;" and replace it with "LineEditStyle(QStyle *style = 0) : QProxyStyle(style)" you can delete the Q_OBJECT, Q_PROPERTY, & "#include "main.moc" which'll shorten and simplify the code. – progro Feb 28 '19 at 20:12
  • @progro No, in Qt the qproperties serve to make explicit that the property exists, besides they are only 2 extra lines – eyllanesc Feb 28 '19 at 20:14
  • OK, I'd like to learn more about that - can you show where in the documentation it says Q_PROPERTY must be declared in this case of a non animated subclass? because there is a very similar code here - https://forum.qt.io/topic/1137/change-qlineedit-text-cursor-shape/4 - on the Qt forums and there is no Q_OBJECT, moc, Q_PROPERTY, so I don't believe it necessary. Besides, it works fine without it and makes compilation much easier. – progro Feb 28 '19 at 20:18
  • @progro I had not seen that post, as I say it does not help in anything to the particular solution but in the reading of the code. On the other hand you have had a problem in the compilation because it seems that you are a newbie in the Qt. In conclusion I do not see that these 2 lines cause problem so I will not eliminate it – eyllanesc Feb 28 '19 at 20:23
  • Lol... it's not necessary but whatever, good luck with your donations! – progro Feb 28 '19 at 20:28
  • I have found more bugs in your code. This is low quality! – progro Feb 28 '19 at 21:49
  • @progro What bugs? Could you be explicit? Perhaps I can correct them – eyllanesc Feb 28 '19 at 21:51