Adding status bar to QTextEdit
-
I have added the following code to QTextEdit constructor.
I have no "status bar " in the window.What am I missing in my code ?
An example of adding / implementing QStatusBar
at run time would helpful.childStatusBar = new QStatusBar();
childStatusBar->setStatusTip(" Show current copy drag and drop text");
childStatusBar->showNormal();
childStatusBar->showMessage("Status test message"); -
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. -
@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
}
-
@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.
-
@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"
-
@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?
-
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
'
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... -
@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.
-
@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". -
Reading seems to be very hard these days. @ChrisW67 already posted the exact code you need but you simply ignore it...
-
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
- creates status bar
- show status bar
- adds text to status bar
- puts the status bar in bottom of the view
- 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();
}
-
@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 ? -
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()
andupdateStatusBarText()
that update the text its two sub-widgets. Then, what you previously had connected toQTextEdit::append()
orQTextEdit::setText()
would be connected your new class' slot instead (maybe create both an update and append slot). -
@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.. -
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).
-
@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?
-
@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 Ehrlicher Lifetime Qt Championreplied to Anonymous_Banned275 on last edited by Christian Ehrlicher
@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.
-
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
-
@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# hasbase
. 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 namedsuper
. You might as well call itfoobar
.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.