Adding status bar to QTextEdit
-
@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.
-
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. -
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.