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. Adding status bar to QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Adding status bar to QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 6 Posters 2.7k 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.
  • Axel SpoerlA Axel Spoerl

    An example is here.
    What exactly is „added to the constructor“?
    The status bar should probably be added to the layout in which the text edit resides.

    A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #3

    @Axel-Spoerl
    I take it I have to "add " layout for "status bar" first.

    OK. I used the following TEST code . it adds "layout" and implements "status bar" ,
    BUT now my text is written over the status text.

    I need to add QLayout *l = new QVBoxLayout(this); but ONLY for "status bar"......

    CCC_MdiChild::CCC_MdiChild()
    {
    setAttribute(Qt::WA_DeleteOnClose);
    isUntitled = true;

    // initialiize utility
    BT_Utility_Library *BTUL = new  BT_Utility_Library();
    
    //installEventFilter(this);
    setMouseTracking(true);
    //add Oct15
    
    QLayout *l = new QVBoxLayout(this);
    l->addWidget(new QTextEdit);
    QStatusBar *b = new QStatusBar;
    l->addWidget(b);
    b->showMessage("XXX");
    l->setMargin(0);
    l->setSpacing(0);
    
    
    b->setStatusTip( "Show current copy drag  and drop text");
    b->showMessage("Status test message");
    

    #ifdef BYPASS

    childStatusBar = new QStatusBar();
    
    
    childStatusBar->setStatusTip(" Show current copy drag  and drop text");
    childStatusBar->showNormal();
    childStatusBar->showMessage("Status test message");
    

    #endif

    }

    1 Reply Last reply
    0
    • Axel SpoerlA Axel Spoerl

      An example is here.
      What exactly is „added to the constructor“?
      The status bar should probably be added to the layout in which the text edit resides.

      A Offline
      A Offline
      Anonymous_Banned275
      wrote on last edited by
      #4

      @Axel-Spoerl The reference RTFM is for MainWIndow which has "status bar" as default. It does not help me to add status bar to QTextEdit as I asked. I am asking for an example , not for RTFM reference - I can do that without help from forum.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #5

        @AnneRanch You do not add or install a QStatusBar into a QTextEdit. You add both the QTextEdit and a QStatusBar to the same (vertical) layout in a containing widget. This is what @Axel-Spoerl's last sentence says. When you RTFM you will find plenty of examples of layout use.

        The four lines you added to the constructor of some unnamed class create a QStatusBar object but fail to put it in a layout. The widget will just be rendered on top of whatever QWidget parent it has (if any).

        Here is the example

        #include <QApplication>
        
        #include <QWidget>
        #include <QLabel>
        #include <QTextEdit>
        #include <QStatusBar>
        #include <QVBoxLayout>
        #include <QTimer>
        
        class Widget : public QWidget
        {
            Q_OBJECT
        
        public:
            Widget(QWidget *parent = nullptr);
            ~Widget();
        
        private slots:
            void addText();
        
        private:
            QTextEdit *m_textEdit;
            QStatusBar *m_statusBar;
            QLabel *m_label;
        };
        
        
        
        Widget::Widget(QWidget *parent)
            : QWidget(parent)
        {
            m_textEdit = new QTextEdit(this);
        
            m_statusBar = new QStatusBar(this);
            m_label = new QLabel("No chars", m_statusBar);
            m_statusBar->addPermanentWidget(m_label);
        
            QVBoxLayout *layout = new QVBoxLayout(this);
            layout->addWidget(m_textEdit);
            layout->addWidget(m_statusBar);
        
            // Just to make something happen and use the status bar
            QTimer *timer = new QTimer(this);
            connect(timer, &QTimer::timeout, this, &Widget::addText);
            timer->setInterval(5000);
            timer->start();
        }
        
        Widget::~Widget()
        {
        }
        
        void Widget::addText()
        {
            m_textEdit->append("More text. ");
            m_statusBar->showMessage("Added more text", 2000);
            m_label->setText(QString("%0 chars").arg(m_textEdit->toPlainText().count()));
        }
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            Widget w;
            w.show();
            return a.exec();
        }
        
        #include "main.moc"
        
        A 1 Reply Last reply
        3
        • C ChrisW67

          @AnneRanch You do not add or install a QStatusBar into a QTextEdit. You add both the QTextEdit and a QStatusBar to the same (vertical) layout in a containing widget. This is what @Axel-Spoerl's last sentence says. When you RTFM you will find plenty of examples of layout use.

          The four lines you added to the constructor of some unnamed class create a QStatusBar object but fail to put it in a layout. The widget will just be rendered on top of whatever QWidget parent it has (if any).

          Here is the example

          #include <QApplication>
          
          #include <QWidget>
          #include <QLabel>
          #include <QTextEdit>
          #include <QStatusBar>
          #include <QVBoxLayout>
          #include <QTimer>
          
          class Widget : public QWidget
          {
              Q_OBJECT
          
          public:
              Widget(QWidget *parent = nullptr);
              ~Widget();
          
          private slots:
              void addText();
          
          private:
              QTextEdit *m_textEdit;
              QStatusBar *m_statusBar;
              QLabel *m_label;
          };
          
          
          
          Widget::Widget(QWidget *parent)
              : QWidget(parent)
          {
              m_textEdit = new QTextEdit(this);
          
              m_statusBar = new QStatusBar(this);
              m_label = new QLabel("No chars", m_statusBar);
              m_statusBar->addPermanentWidget(m_label);
          
              QVBoxLayout *layout = new QVBoxLayout(this);
              layout->addWidget(m_textEdit);
              layout->addWidget(m_statusBar);
          
              // Just to make something happen and use the status bar
              QTimer *timer = new QTimer(this);
              connect(timer, &QTimer::timeout, this, &Widget::addText);
              timer->setInterval(5000);
              timer->start();
          }
          
          Widget::~Widget()
          {
          }
          
          void Widget::addText()
          {
              m_textEdit->append("More text. ");
              m_statusBar->showMessage("Added more text", 2000);
              m_label->setText(QString("%0 chars").arg(m_textEdit->toPlainText().count()));
          }
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              Widget w;
              w.show();
              return a.exec();
          }
          
          #include "main.moc"
          
          A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #6

          @ChrisW67 I have already tried similar code - I think I need to access existing layout - this code blocks the QTextEdit to receive stdout. Adding status bar is "nice to have" , processing stdout is the main function of QTextEdit.

          BY default my implementation of QTextEdit accepts and shows keyboard entry , but I am already changing / stopping keyboard entries by processing stdout. ( so far I have not research how it takes stdout...)

          I have no code to show QTextEdit layout, my implementation of QTextEdit works without any access to layout,
          it is all done by default. I should look at the MDI parent code to make sure....so far I had no need to do that.

          So am I correct to say my code needs to access the existing layout before I can add status bar or anything else?

          C 1 Reply Last reply
          0
          • A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by Anonymous_Banned275
            #7

            GOOD NEWS
            Making slow progress
            The attached code is a modified version of code I have already tried.,
            It is added to class constructor derived from QTextEdit.

            It allows for the main function of the class - read stdout.
            It builds status bar - BUT only its text !
            It shows such text half way in the view - not on the bottom where status bar should be
            and NOT with background color ( grey)

            I still believe the problem is with HOW to access current layout - created by QEditText of
            which I have NO control. I appears that I can only "write over" existing layout.

            I am missing
            how to add this status bar to the bottom of the view
            how to limit the current layout NOT to write the stdout text OVER the status bar.

            This is how it looks

            92ed30da-9bfb-488d-9138-34e33cb224db-image.png '

            auto m_statusBar = new QStatusBar(this);
            auto m_label = new QLabel("Status bar label No chars", m_statusBar);
            m_statusBar->addPermanentWidget(m_label);
            m_statusBar->showMessage("Status test message");
            QVBoxLayout *layout = new QVBoxLayout(this);
            //layout->addWidget(m_textEdit);
            layout->addWidget(m_statusBar);
            

            m_statusBar->showNormal();

            UPDATE
            My MDI main / parent object )MDI example) has code to post messages to its status bar (!) while mouse is over its icons... maybe I can learn how it is done...

            1 Reply Last reply
            0
            • A Anonymous_Banned275

              @ChrisW67 I have already tried similar code - I think I need to access existing layout - this code blocks the QTextEdit to receive stdout. Adding status bar is "nice to have" , processing stdout is the main function of QTextEdit.

              BY default my implementation of QTextEdit accepts and shows keyboard entry , but I am already changing / stopping keyboard entries by processing stdout. ( so far I have not research how it takes stdout...)

              I have no code to show QTextEdit layout, my implementation of QTextEdit works without any access to layout,
              it is all done by default. I should look at the MDI parent code to make sure....so far I had no need to do that.

              So am I correct to say my code needs to access the existing layout before I can add status bar or anything else?

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #8

              @AnneRanch said in Adding status bar to QTextEdit:

              I have already tried similar code - I think I need to access existing layout - this code blocks the QTextEdit to receive stdout.

              If your code is blocking then you need to address that. There is nothing in my example that is blocking.

              So am I correct to say my code needs to access the existing layout before I can add status bar or anything else?

              No. You already have a perfectly good example of how this should be done. The widget you put into your MDI view should be a compound of two widgets in a layout, not a QTextEdit that you then try to shoehorn another widget into. Widgets in the same layout cannot overlap.

              A 1 Reply Last reply
              2
              • C ChrisW67

                @AnneRanch said in Adding status bar to QTextEdit:

                I have already tried similar code - I think I need to access existing layout - this code blocks the QTextEdit to receive stdout.

                If your code is blocking then you need to address that. There is nothing in my example that is blocking.

                So am I correct to say my code needs to access the existing layout before I can add status bar or anything else?

                No. You already have a perfectly good example of how this should be done. The widget you put into your MDI view should be a compound of two widgets in a layout, not a QTextEdit that you then try to shoehorn another widget into. Widgets in the same layout cannot overlap.

                A Offline
                A Offline
                Anonymous_Banned275
                wrote on last edited by
                #9

                @ChrisW67 widget you put into your MDI view should be a compound of two widgets in a layout,
                OK, that is exactly where I need help. As I been saying - I have no access to the QTextEdit "layout" - the resulting view / GUI is showing text - that is how QTextEdit works by default. At present it displays result of QProcess - stdout text.
                So I f can gain the access to the "layout" I should lo be able to add another widget - on your words "compound" .
                The example code just "overlaps" the layout - I need to add to the layout so it won't overlap.
                In other words - limit the current "text layout " size to make room for the "status bar".

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Reading seems to be very hard these days. @ChrisW67 already posted the exact code you need but you simply ignore it...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  A 1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    Anonymous_Banned275
                    wrote on last edited by
                    #11

                    I hope the real contributors to this discussion do not mind if I restart from known, working code .
                    The MainWindow , based on QMainWindow has "status bar".
                    My code app Main WIndow status bar is working as expected.
                    My implementation , renamed copy of app Main Window also works and
                    I have added some tracking code to my version of createStatusBar().
                    This code works as expected.

                    At this point I like to identify WHY the original code line '

                    statusBar()->showMessage(tr("Ready MDI ")); // original 
                    

                    is all what makes it work.

                    A simple "showMessage" is all what

                    1. creates status bar
                    2. show status bar
                    3. adds text to status bar
                    4. puts the status bar in bottom of the view
                    5. set background color of status bar
                      etc....

                    The are no #include <QStatusBar> in application main window,

                    I am asking - how is QMainWindow linked ( NOT a compiler / linker use of term "linker" here )
                    to be able to use
                    statusBar()?

                    PLEASE stay on the subject - comment on the above question - I would appreciate that.

                    void CCC_MainWindow::createStatusBar()
                    {
                    #ifdef TRACE_MDI_XTERM
                    qDebug()<< "TRACE_MDI_XTERM " <<Q_FUNC_INFO;
                    qDebug()<< "TRACE_MDI_XTERM @line " << QString::number(LINE);
                    // qDebug()<< "TRACE_MDI_XTERM constructor @line " << QString::number(LINE);
                    #endif
                    auto m_label = new QLabel("Status bar label No chars",statusBar());
                    statusBar()->addPermanentWidget(m_label);

                    statusBar()->showMessage(tr("Ready MDI ")); // original 
                    
                    statusBar()->setStatusTip(" MDI Process status bar tip");
                    statusBar()->show();
                    

                    }

                    M 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      Reading seems to be very hard these days. @ChrisW67 already posted the exact code you need but you simply ignore it...

                      A Offline
                      A Offline
                      Anonymous_Banned275
                      wrote on last edited by
                      #12

                      @Christian-Ehrlicher To whom do I address a complaint about your insistence of making the discussion difficult for all
                      the participants to track by moving it ?

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • A Anonymous_Banned275

                        I hope the real contributors to this discussion do not mind if I restart from known, working code .
                        The MainWindow , based on QMainWindow has "status bar".
                        My code app Main WIndow status bar is working as expected.
                        My implementation , renamed copy of app Main Window also works and
                        I have added some tracking code to my version of createStatusBar().
                        This code works as expected.

                        At this point I like to identify WHY the original code line '

                        statusBar()->showMessage(tr("Ready MDI ")); // original 
                        

                        is all what makes it work.

                        A simple "showMessage" is all what

                        1. creates status bar
                        2. show status bar
                        3. adds text to status bar
                        4. puts the status bar in bottom of the view
                        5. set background color of status bar
                          etc....

                        The are no #include <QStatusBar> in application main window,

                        I am asking - how is QMainWindow linked ( NOT a compiler / linker use of term "linker" here )
                        to be able to use
                        statusBar()?

                        PLEASE stay on the subject - comment on the above question - I would appreciate that.

                        void CCC_MainWindow::createStatusBar()
                        {
                        #ifdef TRACE_MDI_XTERM
                        qDebug()<< "TRACE_MDI_XTERM " <<Q_FUNC_INFO;
                        qDebug()<< "TRACE_MDI_XTERM @line " << QString::number(LINE);
                        // qDebug()<< "TRACE_MDI_XTERM constructor @line " << QString::number(LINE);
                        #endif
                        auto m_label = new QLabel("Status bar label No chars",statusBar());
                        statusBar()->addPermanentWidget(m_label);

                        statusBar()->showMessage(tr("Ready MDI ")); // original 
                        
                        statusBar()->setStatusTip(" MDI Process status bar tip");
                        statusBar()->show();
                        

                        }

                        M Offline
                        M Offline
                        mchinand
                        wrote on last edited by mchinand
                        #13

                        It's working for your CCC_MainWindow because it inherits from QMainWindow which has a statusBar() method that returns the statusBar for the QMainWindow widget (other widgets don't have this). @ChrisW67 method is the correct way to create a Widget class that consists of a TextEdit with a statusbar. You probably want to add slots to it like updateTextEdit() and updateStatusBarText() that update the text its two sub-widgets. Then, what you previously had connected to QTextEdit::append() or QTextEdit::setText() would be connected your new class' slot instead (maybe create both an update and append slot).

                        A 1 Reply Last reply
                        2
                        • M mchinand

                          It's working for your CCC_MainWindow because it inherits from QMainWindow which has a statusBar() method that returns the statusBar for the QMainWindow widget (other widgets don't have this). @ChrisW67 method is the correct way to create a Widget class that consists of a TextEdit with a statusbar. You probably want to add slots to it like updateTextEdit() and updateStatusBarText() that update the text its two sub-widgets. Then, what you previously had connected to QTextEdit::append() or QTextEdit::setText() would be connected your new class' slot instead (maybe create both an update and append slot).

                          A Offline
                          A Offline
                          Anonymous_Banned275
                          wrote on last edited by
                          #14

                          @mchinand What is being suggested is to replace the existing QTextEdit with generic widget class and it - makes sense. but I was trying to avoid that because it will break the rest of the code (MDI parent /child relations, QProcess etc ) ) including "read stdout ".
                          I still do not understand why I cannot "access " the QTextEdit layout... oh well..

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mchinand
                            wrote on last edited by
                            #15

                            You're not replacing your QTextEdit with a 'generic Widget', it's a widget that has both a QTextEdit and a QStatusBar in a single layout; exactly what you are trying to achieve. Your new widget class will have slots that basically just forward their text arguments to either the QTextEdit or QStatusbar (i.e., the slots will consist of only one line of code).

                            1 Reply Last reply
                            2
                            • A Anonymous_Banned275

                              @Christian-Ehrlicher To whom do I address a complaint about your insistence of making the discussion difficult for all
                              the participants to track by moving it ?

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @AnneRanch said in Adding status bar to QTextEdit:

                              the participants to track by moving it ?

                              I only said that @mchinand already posted the correct solution (which you're ignoring the whole time) - so how should this distract other users here?

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              A 1 Reply Last reply
                              2
                              • Christian EhrlicherC Christian Ehrlicher

                                @AnneRanch said in Adding status bar to QTextEdit:

                                the participants to track by moving it ?

                                I only said that @mchinand already posted the correct solution (which you're ignoring the whole time) - so how should this distract other users here?

                                A Offline
                                A Offline
                                Anonymous_Banned275
                                wrote on last edited by
                                #17

                                @mchinand To all who contributed to this thread - thanks very much, appreciate all.
                                There has been MAJOR fault with most all of the suggestions - they are OK in THEORY.
                                I have said somewhere that I like to ADD status bar to WORKING code. I will not dicker about meaning of "replacing" , but the existing QTexrEdit is a MDI child and replacing it with QWidget as suggested "breaks the code" big time .
                                Adding "status bar " would be nice to have , but I am not willing to spent time to fix the broken code for "nice to have".
                                Again I do appreciate all the help rendered, but it is time to call it quits.

                                CASE CLOSED.

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • A Anonymous_Banned275

                                  @mchinand To all who contributed to this thread - thanks very much, appreciate all.
                                  There has been MAJOR fault with most all of the suggestions - they are OK in THEORY.
                                  I have said somewhere that I like to ADD status bar to WORKING code. I will not dicker about meaning of "replacing" , but the existing QTexrEdit is a MDI child and replacing it with QWidget as suggested "breaks the code" big time .
                                  Adding "status bar " would be nice to have , but I am not willing to spent time to fix the broken code for "nice to have".
                                  Again I do appreciate all the help rendered, but it is time to call it quits.

                                  CASE CLOSED.

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by Christian Ehrlicher
                                  #18

                                  @AnneRanch said in Adding status bar to QTextEdit:

                                  I have said somewhere that I like to ADD status bar to WORKING code. I will not dicker about meaning of "replacing" , but the existing QTexrEdit is a MDI child and replacing it with QWidget as suggested "breaks the code" big time .

                                  And everybody told you (even on so) from the beginning that this is not possible and you have to create a new widget which contains the two widgets. That's the way to go with every compound widget you want to create.

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  1 Reply Last reply
                                  2
                                  • A Offline
                                    A Offline
                                    Anonymous_Banned275
                                    wrote on last edited by
                                    #19

                                    Would anybody in this forum be interested in having a discussion about using "super"?

                                    https://stackoverflow.com/questions/180601/using-super-in-c

                                    I have received the following suggestion to resolve the QTextEdit / status bar issue.
                                    I have never heard of using "super" , and it is little confusing to me since QTextEdit is a base class for my "child" class.

                                    The attached code is using setLineWrapMode( and claims that method is inherited by QTextEdit.

                                    I have not tried to implement this code in my app.

                                    #include <QApplication>
                                    #include <QScrollBar>
                                    #include <QStatusBar>
                                    #include <QTextEdit>
                                    
                                    namespace {
                                      class text_edit: public QTextEdit {
                                        using super = QTextEdit;
                                      public:
                                        explicit text_edit (QWidget *parent = nullptr)
                                          : super(parent)
                                          , m_status(this)
                                          {
                                            m_status.setStyleSheet("background-color: gray;");
                                            m_status.showMessage("Status text goes here...");
                                            show_status(true);
                                            horizontalScrollBar()->installEventFilter(this);
                                            verticalScrollBar()->installEventFilter(this);
                                            setLineWrapMode(QTextEdit::NoWrap);
                                          }
                                      protected:
                                        virtual bool eventFilter (QObject *obj, QEvent *event) override
                                          {
                                            if (event->type() == QEvent::Show || event->type() == QEvent::Hide)
                                              update_status_geometry();
                                            return super::eventFilter(obj, event);
                                          }
                                        virtual void resizeEvent (QResizeEvent *event) override
                                          {
                                            super::resizeEvent(event);
                                            update_status_geometry();
                                          }
                                      private:
                                        void show_status (bool on)
                                          {
                                            if (on) {
                                              setViewportMargins(0, 0, 0, m_status.height());
                                              m_status.show();
                                            } else {
                                              setViewportMargins(0, 0, 0, 0);
                                              m_status.hide();
                                            }
                                          }
                                        void update_status_geometry ()
                                          {
                                    
                                            /*
                                             * Calculate initial geometry of the QStatusBar based on its size hint.
                                             */
                                            auto s = m_status.sizeHint();
                                            s.rwidth() = width();
                                            QRect geom(QPoint(0, 0), s);
                                            geom.moveTop(height() - s.height());
                                    
                                            /*
                                             * Adjust the status bar geometry to allow for the scroll bars.
                                             */
                                            if (horizontalScrollBar()->isVisible())
                                              geom.moveTop(geom.top() - horizontalScrollBar()->height());
                                            if (verticalScrollBar()->isVisible())
                                              geom.setRight(geom.right() - verticalScrollBar()->width());
                                            m_status.setGeometry(geom);
                                          }
                                        QStatusBar m_status;
                                      };
                                    }
                                    
                                    int
                                    main (int argc, char **argv)
                                    {
                                      QApplication app(argc, argv);
                                      text_edit te;
                                      te.show();
                                      return app.exec();
                                    }
                                    #endif
                                    #endif // STATUS_BAR_NOTES_COPY_H
                                    
                                    Chris KawaC 1 Reply Last reply
                                    0
                                    • A Anonymous_Banned275

                                      Would anybody in this forum be interested in having a discussion about using "super"?

                                      https://stackoverflow.com/questions/180601/using-super-in-c

                                      I have received the following suggestion to resolve the QTextEdit / status bar issue.
                                      I have never heard of using "super" , and it is little confusing to me since QTextEdit is a base class for my "child" class.

                                      The attached code is using setLineWrapMode( and claims that method is inherited by QTextEdit.

                                      I have not tried to implement this code in my app.

                                      #include <QApplication>
                                      #include <QScrollBar>
                                      #include <QStatusBar>
                                      #include <QTextEdit>
                                      
                                      namespace {
                                        class text_edit: public QTextEdit {
                                          using super = QTextEdit;
                                        public:
                                          explicit text_edit (QWidget *parent = nullptr)
                                            : super(parent)
                                            , m_status(this)
                                            {
                                              m_status.setStyleSheet("background-color: gray;");
                                              m_status.showMessage("Status text goes here...");
                                              show_status(true);
                                              horizontalScrollBar()->installEventFilter(this);
                                              verticalScrollBar()->installEventFilter(this);
                                              setLineWrapMode(QTextEdit::NoWrap);
                                            }
                                        protected:
                                          virtual bool eventFilter (QObject *obj, QEvent *event) override
                                            {
                                              if (event->type() == QEvent::Show || event->type() == QEvent::Hide)
                                                update_status_geometry();
                                              return super::eventFilter(obj, event);
                                            }
                                          virtual void resizeEvent (QResizeEvent *event) override
                                            {
                                              super::resizeEvent(event);
                                              update_status_geometry();
                                            }
                                        private:
                                          void show_status (bool on)
                                            {
                                              if (on) {
                                                setViewportMargins(0, 0, 0, m_status.height());
                                                m_status.show();
                                              } else {
                                                setViewportMargins(0, 0, 0, 0);
                                                m_status.hide();
                                              }
                                            }
                                          void update_status_geometry ()
                                            {
                                      
                                              /*
                                               * Calculate initial geometry of the QStatusBar based on its size hint.
                                               */
                                              auto s = m_status.sizeHint();
                                              s.rwidth() = width();
                                              QRect geom(QPoint(0, 0), s);
                                              geom.moveTop(height() - s.height());
                                      
                                              /*
                                               * Adjust the status bar geometry to allow for the scroll bars.
                                               */
                                              if (horizontalScrollBar()->isVisible())
                                                geom.moveTop(geom.top() - horizontalScrollBar()->height());
                                              if (verticalScrollBar()->isVisible())
                                                geom.setRight(geom.right() - verticalScrollBar()->width());
                                              m_status.setGeometry(geom);
                                            }
                                          QStatusBar m_status;
                                        };
                                      }
                                      
                                      int
                                      main (int argc, char **argv)
                                      {
                                        QApplication app(argc, argv);
                                        text_edit te;
                                        te.show();
                                        return app.exec();
                                      }
                                      #endif
                                      #endif // STATUS_BAR_NOTES_COPY_H
                                      
                                      Chris KawaC Offline
                                      Chris KawaC Offline
                                      Chris Kawa
                                      Lifetime Qt Champion
                                      wrote on last edited by Chris Kawa
                                      #20

                                      @AnneRanch said:

                                      Would anybody in this forum be interested in having a discussion about using "super"?

                                      It's an idea from other languages. Java has super, C# has base. All it does is give you a name of the base class when you're subclassing something. There's no such thing in C++, so this is a little trick to emulate that feature with an alias (using keyword). Since it's just an alias it doesn't have to be named super. You might as well call it foobar.

                                      The idea of using this pattern is that if you decide to change the class you're inheriting from you don't have to "fix" all the other places where you used your base class name, just the alias. It also protects from a common bug that happens when you're changing the inheritance structure. For example if you have code like this:

                                      class MyClass : public QWidget
                                      {
                                         void mousePressEvent(QMouseEvent* evt) override
                                         {
                                            QWidget::mousePressEvent(evt); //calling base implementation
                                         }
                                      }
                                      

                                      and later decide to change the inheritance to

                                      class MyClass : public QPushButton
                                      

                                      but forget to also change the implementation of mousePressEvent to call the QPushButton implementation, this code will still compile and sometimes even work correctly if the new class implementation doesn't do anything special. But sometimes you're skipping important code and it's a difficult to find bug, because everything is legal, compiles and runs without warning. It just doesn't do what you wanted.

                                      If you use an alias like this:

                                      class MyClass : public QWidget
                                      {
                                         using foobar = QWidget;
                                         void mousePressEvent(QMouseEvent* evt) override
                                         {
                                            foobar::mousePressEvent(evt); //calling base implementation
                                         }
                                      }
                                      

                                      You only have to change the alias and it will be ok everywhere. Of course you can still forget to change the alias too, but it's just one place instead of every other usage of the class name.

                                      That being said it's just a code style, so matter of opinion if you should use it or not. It has some benefits but I find it not a big deal in practice. Personally I don't use it, but if I'm adding something to an existing codebase that does, I will for consistency.

                                      1 Reply Last reply
                                      2
                                      • M Offline
                                        M Offline
                                        mchinand
                                        wrote on last edited by mchinand
                                        #21

                                        Using the example code in your post about super to add a statusbar to a QTextEdit looks more complicated than the compound widget approach. It's still not clear to me why you think that it will be so hard to modify your code to use a custom compound widget in place of a QTextEdit.

                                        1 Reply Last reply
                                        3
                                        • Chris KawaC Offline
                                          Chris KawaC Offline
                                          Chris Kawa
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #22

                                          super has nothing to do with the status bar issue. It's just someone's code style. You can do the same without it.

                                          And yes, the posted solution has issues, hardcoded values and styles and is generally mega-super-ugly. Whatever hackery code you can come up with is gonna have issues and be worse than the correct solution - put text edit and status bar separately in a container widget. That is how you're supposed to compose widgets in Qt, whether you like it or not. It's just a couple lines of code more, certainly a lot easier than that thing.

                                          1 Reply Last reply
                                          4

                                          • Login

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