[SOLVED] The tip message isn't shown up in QStatusBar ?
-
Dear Forumers, the menu item tips aren't shown up in QStatusBar but showMessage works properly. I cann't figure out where I am wrong. Could you please be so kind to help me ? Thank you in advance for any help and advices.
//viral.cpp
@
#include "viral.h"viral::viral(QWidget *parent)
: QWidget(parent) {
setWindowTitle("Writeboard");
edit = new QTextEdit();
bar = new QMenuBar(this);
status = new QStatusBar(this);menubar0 = new QMenu(this);
menubar1 = new QMenu(this);bar -> setMinimumWidth(2);
layout_V = new QVBoxLayout();
//menu bar 0
newFile = new QAction(("&New"),this);
newFile -> setShortcuts(QKeySequence::New);
newFile -> setStatusTip(tr("Create a new file"));
connect(newFile,SIGNAL(triggered()),this,SLOT(newfile()));
//menu bar 1
mystatus = new QAction(("&Status Bar"), this);
mystatus -> setCheckable(true);
connect( mystatus, SIGNAL(toggled(bool)), status, SLOT(setHidden(bool)));menubar0 = bar -> addMenu(tr("&File"));
menubar1 = bar -> addMenu(tr("&View"));menubar0 -> addAction(newFile);
menubar1 -> addAction(mystatus);layout_V -> addWidget(bar);
layout_V -> addWidget(edit);
layout_V -> addWidget(status);
setLayout(layout_V);
showMaximized();
}viral::~viral() {}
void viral::newfile() {
edit -> setPlainText("");
mFilePath = "";
status -> showMessage(tr("New File created"),3000);
}
@
//viral.h
@
#ifndef VIRAL_H
#define VIRAL_H#include <QApplication>
#include <QtGui/QWidget>
#include<QMenuBar>
#include<QAction>
#include<QTextEdit>
#include<QVBoxLayout>
#include<QMenu>
#include<QStatusBar>
#include<QFile>
#include<QFileDialog>
#include<QString>
#include<QMessageBox>class viral : public QWidget
{
Q_OBJECTpublic:
viral(QWidget *parent = 0);
~viral();private:
QStatusBar *status;
QTextEdit *edit;
QMenu *menubar0;
QMenu *menubar1;
QMenuBar *bar;QAction *newFile;
QAction *mystatus;
QVBoxLayout *layout_V;
private slots:
void newfile();
private:
QString mFilePath;
};#endif // VIRAL_H
@
//main.cpp
@
#include "viral.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
viral w;
w.show();
return a.exec();
}
@ -
Hi,
You should add which Qt/OS combo you are using
-
This one is a bit old, can you test if you have the same behavior with 4.8.6 ?
-
Hi DiIvPa,
SGaist : I guess this problem is not with Qt version but related with QMainWindow & QWidget.
DiIvPa : If you check QMainWindow has statusBar() function which returns QStatusBar * and as per documentation setStatusTip(QString) will work on that perfectly.
But if you try the same thing on QWidget you will not find statusBar() function as member of QWidget.Here is solution for your problem....
Modified code as....
@
// ---- viral.cpp ---#include "viral.h"
viral::viral(QWidget *parent)
: QWidget(parent) {
setWindowTitle("Writeboard");
edit = new QTextEdit();
bar = new QMenuBar(this);
status = new QStatusBar(this);menubar0 = new QMenu(this);
menubar1 = new QMenu(this);bar -> setMinimumWidth(2);
layout_V = new QVBoxLayout();
//menu bar 0
newFile = new QAction(("&New"),this);
newFile -> setShortcuts(QKeySequence::New);
newFile -> setStatusTip(tr("Create a new file"));
connect(newFile,SIGNAL(triggered()),this,SLOT(newfile()));//menu bar 1
mystatus = new QAction(("&Status Bar"), this);
mystatus -> setCheckable(true);
mystatus->setStatusTip(tr("Status check box here..."));
connect( mystatus, SIGNAL(toggled(bool)), status, SLOT(setHidden(bool)));
connect( newFile, SIGNAL(hovered()), this, SLOT(newFileStatusMsg()));
connect( mystatus, SIGNAL(hovered()), this, SLOT(myStatusMsg()));menubar0 = bar -> addMenu(tr("&File"));
menubar1 = bar -> addMenu(tr("&View"));menubar0 -> addAction(newFile);
menubar1 -> addAction(mystatus);layout_V -> addWidget(bar);
layout_V -> addWidget(edit);
layout_V -> addWidget(status);
status->activateWindow();
setLayout(layout_V);
showMaximized();
}viral::~viral() {}
void viral::newFileStatusMsg()
{
QString qString1 = newFile->statusTip();
QByteArray byteArray = qString1.toUtf8();
const char* cString = byteArray.constData();
status -> showMessage(tr(cString),3000);
}void viral::myStatusMsg()
{
QString qString1 = mystatus->statusTip();
QByteArray byteArray = qString1.toUtf8();
const char* cString = byteArray.constData();
status -> showMessage(tr(cString),3000);
}void viral::newfile() {
edit -> setPlainText("");
mFilePath = "";
status -> showMessage(tr("New File created"),3000);
}
// --- viral.h ---
#ifndef VIRAL_H
#define VIRAL_H
#include <QApplication>#include<QMenuBar>
#include<QAction>
#include<QTextEdit>
#include<QVBoxLayout>
#include<QMenu>
#include<QStatusBar>
#include<QFile>
#include<QFileDialog>
#include<QString>
#include<QMessageBox>class viral : public QWidget
{
Q_OBJECTpublic:
viral(QWidget *parent = 0);
~viral();private:
QStatusBar *status;
QTextEdit *edit;
QMenu *menubar0;
QMenu *menubar1;
QMenuBar *bar;QAction *newFile;
QAction *mystatus;
QVBoxLayout *layout_V;
private slots:
void newfile();
void newFileStatusMsg();
void myStatusMsg();private:
QString mFilePath;
};#endif // VIRAL_H
// ---main.cpp ---
#include "viral.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
viral w;
w.show();return a.exec();
}
@I have complied and run now it will work fine.... as you want... !
Thanks
Prashant[edit: corrected code formatting (one @ a the beginning and one at the end SGaist]
-
Pardone me for the formatting of code....
I will defintely take care from onwards... -
Good catch ! I've missed the wrong base widget
-
Dear Prashant, SGaist, in some UI I used staus bar of QMainWindow and it works fine. Then I tryed to write separate widget with status bar and encounted problem with setStatusTip. I tryed really different CONNECT but wasn't successful (I tryed use the signal message Change and so on). Now I see that I need to define separate connections to every menu item. Not good but .... As to the Qt versions then I use the standard version from Debian distributive and to use "third party version" isn't a good practice. But I belive that the current version is modern enough because I didn't meet any discrepancy with manuals yet and so on...
Ok, I think that the problem could be considered as SOLVED. Thank you very much for help. I really appreciate it. My best wished and regards. -
Your welcome....!