Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change QLineEdit text cursor shape
Forum Updated to NodeBB v4.3 + New Features

Change QLineEdit text cursor shape

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 11.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brisco
    wrote on last edited by
    #1

    Hi All,

    Is there any way to change the blinking text cursor shape in
    a QLineEdit widget? Such as, to change the shape of the cursor
    from thin vertical line to a block style.

    Thanks

    Brisco

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by A Former User
      #2

      Well, you can customize QStyle in order to return a different cursor size for QStyle::PM_TextCursorWidth

      Example for a 10-pixel large cursor ( just an idea, it's not written in a clean way :) ):

      class LineEditStyle : public QCommonStyle
      {
      public:
        int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const;
      };
      
      class LineEdit : public QLineEdit
      {
          Q_OBJECT
      
      public:
          LineEdit(QWidget *parent = 0);
      };
      
      int LineEditStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
      {
        if (metric == QStyle::PM_TextCursorWidth)
          return 10;
      
        return QCommonStyle::pixelMetric(metric,option,widget);
      }
      
      LineEdit::LineEdit(QWidget *parent)
          : QLineEdit(parent)
      {
        setStyle(new LineEditStyle);
      }
      

      Allocate a LineEdit and it should work :) AFAIK, you can change the width, but I haven't found any other properties.

      Antonio.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tony
        wrote on last edited by A Former User
        #3

        Hey, I didn't know that there's QProxyStyle class, that lets you implement the above solution in a better way :)

        class LineEditStyle : public QProxyStyle
        {
        public:
          LineEditStyle(QStyle *style = 0) : QProxyStyle(style) { }
        
          int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const;
        };
        
        class LineEdit : public QLineEdit
        {
          Q_OBJECT
        
        public:
          LineEdit(QWidget *parent = 0);
        };
        
        int LineEditStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
        {
          if (metric == QStyle::PM_TextCursorWidth)
            return 10;
        
          return QProxyStyle::pixelMetric(metric,option,widget);
        }
        
        LineEdit::LineEdit(QWidget *parent)
            : QLineEdit(parent)
        {
          setStyle(new LineEditStyle(style()));
        }
        

        So it will adapt perfectly to your default style for QLineEdit.

        1 Reply Last reply
        1
        • B Offline
          B Offline
          brisco
          wrote on last edited by
          #4

          Antonio,

          It works.

          Thanks for your helpful reply.

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #5

            Why I can't apply this solution on a QTextEdit's cursor?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved