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. Regarding the mirror effect required for text in QLabel
Qt 6.11 is out! See what's new in the release blog

Regarding the mirror effect required for text in QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 5.2k Views 4 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.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by
    #1

    Hi,

    I require the text in QLabel to be a mirror reflection format, so i went on some examples they provided for image in QLabel, can we have text in similar format, apart from image. Because i am using text in QLabel, can we have it, if yes can anyone guide me.

    Thanks,

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      What is to "a mirror reflection format" ?
      Some sort of Text effect using real text instead of image ?

      1 Reply Last reply
      1
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by Pradeep Kumar
        #3
        This post is deleted!
        mrjjM 1 Reply Last reply
        0
        • Pradeep KumarP Pradeep Kumar

          This post is deleted!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Pradeep-Kumar
          Not seeing the image / link?

          1 Reply Last reply
          1
          • M Offline
            M Offline
            muratkarakus7
            wrote on last edited by
            #5

            Wouldn't be easier to inherit from QLabel and override set text function. In that way, you can do custom processing for it.

            1 Reply Last reply
            2
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by
                #7

                sorry for the inconvenience

                Here is the link

                https://postimg.org/image/d727f14gb/

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  muratkarakus7
                  wrote on last edited by
                  #8

                  My solution would be overriding paintEvent of QLabel. You have to rotate text. It is a bit hard but you can start from qt source code to check default implementation of paintEvent and make a small change to rotate it. After that you can have another widget one with normal label big font, other with small font rotated text, put them into a layout and present to user. Or, if you can do all this in a label paintEvent.

                  http://stackoverflow.com/questions/26511409/qt-creator-rotate-a-qlabel-that-contains-text-90-degrees

                  1 Reply Last reply
                  2
                  • Pradeep KumarP Offline
                    Pradeep KumarP Offline
                    Pradeep Kumar
                    wrote on last edited by
                    #9

                    I will give it a try guys. Will update.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      muratkarakus7
                      wrote on last edited by muratkarakus7
                      #10

                      Thanks!

                      I noted as well, I will try when I have time. In case I come up with a good looking widget, I will share with you guys.

                      mrjjM 1 Reply Last reply
                      2
                      • M muratkarakus7

                        Thanks!

                        I noted as well, I will try when I have time. In case I come up with a good looking widget, I will share with you guys.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi
                        The hard part is most likely to get good looking reflection.
                        Some gradient and transparency will help but will take some playing around to look
                        nice. :)

                        1 Reply Last reply
                        1
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          Hi! Maybe a bit over-engineered, but I'd use a custom QGraphicsEffect for this. The wiki article Text Shadows in QLabel shows how to apply it.

                          1 Reply Last reply
                          3
                          • Pradeep KumarP Offline
                            Pradeep KumarP Offline
                            Pradeep Kumar
                            wrote on last edited by
                            #13

                            Guys with your help i have compiled the following code.

                            I have used 2 classes
                            Follow is the link where the code will be available.

                            By clicking QPushButton , i am showing QLabel.

                            classes i have used
                            LabelShadowEffect.h
                            LabelShadowEffect.cpp

                            http://codepaste.net/dke2fr
                            http://codepaste.net/jec1hm

                            and i have used LabelRotation Class where i have inherited from QLabel .
                            http://codepaste.net/gdvi11
                            http://codepaste.net/tqo8bn

                            Guys help me with the code where i require the alphabets to be reflected, but in the output, it is not.

                            Below is the link of the output

                            https://postimg.org/image/v13g6x943/

                            Guidance is required.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              muratkarakus7
                              wrote on last edited by
                              #14

                              For the LabelRotation, I guess you should use righttoleft direction, then it will be how you want.

                              layoutDirection : Qt::LayoutDirection
                              This property holds the layout direction for this widget.
                              By default, this property is set to Qt::LeftToRight.

                              1 Reply Last reply
                              0
                              • Pradeep KumarP Offline
                                Pradeep KumarP Offline
                                Pradeep Kumar
                                wrote on last edited by
                                #15

                                I used below line but still didnt work

                                painter.setLayoutDirection(Qt::RightToLeft);

                                can u share the piece of code or edit in the code which i provided in the link. I will also try to make it work.

                                1 Reply Last reply
                                0
                                • ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by A Former User
                                  #16

                                  Here is an example using QGraphicsEffect. The result looks like this:

                                  mainwindow.cpp

                                  //...
                                  #include "myeffect.h"
                                  
                                  MainWindow::MainWindow(QWidget *parent) :
                                      QMainWindow(parent),
                                      ui(new Ui::MainWindow)
                                  {
                                      ui->setupUi(this);
                                      MyEffect *effect = new MyEffect(this);
                                      ui->label->setGraphicsEffect(effect);
                                  }
                                  //...
                                  

                                  myeffect.h

                                  #ifndef MYEFFECT_H
                                  #define MYEFFECT_H
                                  
                                  #include <QGraphicsEffect>
                                  
                                  class MyEffect : public QGraphicsEffect
                                  {
                                      Q_OBJECT
                                  public:
                                      MyEffect(QObject *parent = Q_NULLPTR);
                                      virtual QRectF boundingRectFor(const QRectF &sourceRect) const override;
                                      virtual void draw(QPainter *painter) override;
                                  };
                                  
                                  #endif // MYEFFECT_H
                                  

                                  myeffect.cpp

                                  #include "myeffect.h"
                                  
                                  #include <QPainter>
                                  #include <QPixmap>
                                  #include <QPoint>
                                  #include <QSize>
                                  
                                  MyEffect::MyEffect(QObject *parent)
                                      : QGraphicsEffect(parent)
                                  {
                                  }
                                  
                                  QRectF MyEffect::boundingRectFor(const QRectF &sourceRect) const
                                  {
                                      return sourceRect.adjusted(0, 0, 0, sourceRect.height());
                                  }
                                  
                                  void MyEffect::draw(QPainter *painter)
                                  {
                                      painter->save();
                                      QPoint offset;
                                      const QPixmap pixmap = sourcePixmap(Qt::LogicalCoordinates, &offset);
                                      painter->drawPixmap(offset, pixmap);
                                  
                                      QImage img = pixmap.toImage();
                                      const QSize s = img.size();
                                      const qreal fac = 255.0 / s.height();
                                      const qreal brightness = 0.9;
                                      for (int i=0; i<s.width(); ++i) {
                                          for (int j=0; j<s.height(); ++j) {
                                              QRgb argb = img.pixel(i,j);
                                              int grayValue = (255-(fac*j))*brightness;
                                              grayValue = grayValue < 0 ? 0 : grayValue;
                                              grayValue = grayValue > 255 ? 255 : grayValue;
                                              QColor newColor(grayValue, grayValue, grayValue, qAlpha(argb));
                                              img.setPixelColor(i,j,newColor);
                                          }
                                      }
                                      img = img.mirrored();
                                      painter->drawImage(0,0, img);
                                      painter->restore();
                                  }
                                  

                                  Hope it helps!

                                  1 Reply Last reply
                                  2
                                  • Pradeep KumarP Offline
                                    Pradeep KumarP Offline
                                    Pradeep Kumar
                                    wrote on last edited by
                                    #17

                                    Thanks man
                                    I got the output , here is the link of the output which got.

                                    https://postimg.org/image/qtwp1ywst/

                                    Thanks,

                                    1 Reply Last reply
                                    1
                                    • Pradeep KumarP Offline
                                      Pradeep KumarP Offline
                                      Pradeep Kumar
                                      wrote on last edited by
                                      #18

                                      Thanks guys @muratkarakus7 @mrjj @Wieland for your support, marking as solved, which will be useful.

                                      Thanks.

                                      1 Reply Last reply
                                      3

                                      • Login

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