How to disable tooltips on QToolbar? - c++

How does one disable tooltips on a Qt4 QToolBar?
Using QAction::setToolTip("") does nothing and I can't find any settings related to disabling tooltips on either a QAction or QToolbar!
Example:
Toolbar.h
#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <QtGui>
class Toolbar : public QToolBar
{
Q_OBJECT
public:
Toolbar()
{
QAction *action = this->addAction("Action");
action->setToolTip("");
}
bool event(QEvent *event)
{
if(event->type() == QEvent::ToolTip)
{
qDebug() << "QEvent::ToolTip";
}
return QToolBar::event(event);
}
};
#include "moc_Toolbar.cpp"
#endif // TOOLBAR_H
main.cpp
#include <QtGui>
#include "Toolbar.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
Toolbar *toolbar = new Toolbar;
window.addToolBar(toolbar);
window.setCentralWidget(new QWidget());
window.show();
return app.exec();
}

An event filter has to be used in this scenario.
Toolbar.h
#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <QtGui>
class Toolbar : public QToolBar
{
Q_OBJECT
public:
Toolbar()
{
QAction *action = this->addAction("Action");
}
bool eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::ToolTip)
{
return true;
}
return false;
}
};
#include "moc_Toolbar.cpp"
#endif // TOOLBAR_H
main.cpp
#include <QtGui>
#include "Toolbar.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
Toolbar *toolbar = new Toolbar;
qApp->installEventFilter(toolbar);
window.addToolBar(toolbar);
window.setCentralWidget(new QWidget());
window.show();
return app.exec();
}
I'm not quite sure how to localize this to just the Toolbar but I don't like tooltips anyway so this is a quick way to disable all of them.

Related

QTabwidget click tab event C++

I am new in QT 4 C++ .I have QT 4 form with QTabwidget like in th picture below.
enter image description here
I want to disply on console the string "aa" by selecting tab on clicking it.
Here is my newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
#include "ui_newForm.h"
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
virtual ~newForm();
private:
Ui::newForm widget;
};
#endif /* _NEWFORM_H */
_________________________________________________________________________________________
Here is my main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm *a = new newForm();
a->show();
// create and show your widgets here
return app.exec();
}
Here is my newForm.cpp
#include "newForm.h"
#include <iostream>
#include <QDebug>
newForm::newForm() {
connect(widget.tabWidget, SIGNAL(stateChanged()), this, SLOT(onTabChanged(int)));
widget.setupUi(this);
}
newForm::~newForm() {
}
void newForm::onTabChanged(int ){
qDebug()<<"aa"<<"\n";
}
On selecting new tab it is not displaying "aa"?How to qDebug() "aa" on console?
First of all, why are you using Qt 4 in 2022?
Next thing use currentIndexChanged(int) istead of stateChanged().
Did you also noticed that stateChanged() doesnt pass an interger which is needed for onTabChanged(int).
You also connected widget.tabWidget which isn't initilized yet.
This code might work:
newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
namespace Ui { class newForm; };
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
~newForm();
private:
Ui::newForm *widget;
};
#endif /* _NEWFORM_H */
newForm.cpp
#include "newForm.h"
#include "ui_newForm.h"
#include <QDebug>
newForm::newForm()
: widget(new Ui::newForm)
{
widget->setupUi(this);
connect(widget->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
}
void newForm::onTabChanged(int ){
qDebug() << "aa";
}
newForm::~newForm() {
delete widget;
}
main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm a;
a.show();
// create and show your widgets here
return app.exec();
}

how to use scene object of main file in other file?

I tried to make tictactoe game using Qt and Qgraphicsview but when I draw x on board using Graphicstextitem in mousePressEvent , X does not appear. how fix that ?
I think the problem is that scene of textitem Different from scene of main file but I do not know how fix that.
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Game *gm = new Game;
Game *rect1=new Game;
gm->scenc->setSceneRect(0,0,800,600);
rect1->setRect(160,100,150,150);
gm->scenc->addItem(rect1);
gm->view->setScene(gm->scenc);
gm->view->show();
return a.exec();
}
in game.cpp:
#include <game.h>
Game::Game()
{
scenc= new QGraphicsScene;
view = new QGraphicsView;
text= new QGraphicsTextItem;
}
void Game::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton )
{
text->setPlainText("X");
text->setFont(QFont("Tahoma",24));
text->setPos((160+160+120)/2,140);
scenc->addItem(text);
}
}
in game.h :
class Game : public QObject , public QGraphicsRectItem
{
Q_OBJECT
public:
Game();
QGraphicsScene *scenc;
QGraphicsView *view;
QGraphicsTextItem *text;
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};
The following example illustrates how to do it the right way. Firstly, you have to notice, that Game::mousePressEvent doesn't override any virtual function. It's a good habit to use the override keyword and to drop the virtual keyword in order to be sure, that a virtual function is overwritten.
Game is not derived from QGraphicsScene and has therefore no mousePressEvent member.
Try the following example app.
MyScene.h
#pragma once
#include <QWidget>
#include <QDebug>
#include <QHBoxLayout>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QGraphicsSceneMouseEvent>
class MyScene : public QGraphicsScene {
Q_OBJECT
public:
MyScene(QWidget* parent = nullptr) : QGraphicsScene(parent) {
setSceneRect(0, 0, 800, 600);
}
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* mouseEvent) override {
if (mouseEvent->buttons() == Qt::LeftButton)
{
auto text = new QGraphicsTextItem;
addItem(text);
text->setPlainText("X");
text->setPos(mouseEvent->scenePos());
}
}
private:
QGraphicsView* mView;
QGraphicsTextItem* mText;
};
main.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include "MyScene.h"
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto scene = new MyScene;
auto view = new QGraphicsView;
view->setScene(scene);
view->show();
return a.exec();
}

Qt (C++) Pass Variable from Dialog to main.cpp

When i start my Program a Dialog-Window pops up and asks me to enter a name. Once i've entered my Name and press the Button it closes the Dialog and opens the main window.
My question is how i get the variable/ Name i just set in the Dialog into another class / my main.cpp
Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
#include <QtNetwork>
#include <sstream>
#include "mydialog.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Open Dialog
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
//Open Main Window
GW2::MainWindow w;
w.show();
return a.exec();
}
mydialog.cpp
#include "mydialog.h"
#include "ui_mydialog.h"
#include <QDebug>
using namespace std;
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyDialog)
{
ui->setupUi(this);
}
MyDialog::~MyDialog()
{
delete ui;
}
void MyDialog::on_pushButton_clicked()
{
QString MYNAME = ui->lineEdit->text();
close();
}
I can get MYNAME here that works after i press the Button but i need to pass the Variable...
mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
#include <QString>
namespace Ui {
class MyDialog;
}
class MyDialog : public QDialog
{
Q_OBJECT
public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();
private slots:
void on_pushButton_clicked();
private:
Ui::MyDialog *ui;
};
#endif // MYDIALOG_Hs
I tried using google and search function but didn'T find anything that worked on my project. Hope you can help me. Cheers
Add this in MyDialog:
QString MyDialog::getName()
{
return ui->lineEdit->text();
}
Then do:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Open Dialog
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
// retrieve the name
QString name = mDialog.getName();
//Open Main Window
GW2::MainWindow w;
w.show();
return a.exec();
}
Note that the dialog could be canceled. You should call accept() rather than close() from on_pushButton_clicked() and later test if the dialog was accepted or not:
if ( mDialog.exec() == QDialog::Accepted )
{
QString name = mDialog.getName();
...

Qt events or repaint() function not working

The key event listener or the function repaint() are not working, I've tried calling paintEvent(QPaintEvent *) directly but it didn't work either, thanks to further answers.
Here's the code:
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QRectF>
namespace Ui {
class Window;
}
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
~Window();
void paintEvent(QPaintEvent *);
void keyPressEvent(QKeyEvent * );
private:
Ui::Window *ui;
QRectF player;
QPainter * painter = new QPainter(this);
};
#endif // WINDOW_H
window.cpp
#include "window.h"
#include "ui_window.h"
Window::Window(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window)
{
ui->setupUi(this);
player.setX(0);
player.setY(0);
player.setWidth(50);
player.setHeight(50);
}
Window::~Window()
{
delete ui;
}
void Window::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(QPen(Qt::black, 3, Qt::DashDotLine, Qt::RoundCap));
painter.setBrush(QBrush(Qt::blue));
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawEllipse(player.x(), player.y(), player.width(), player.height());
}
void Window::keyPressEvent(QKeyEvent * event)
{
if(event->type() == Qt::Key_W)
player.setY(player.y() - 1);
if(event->type() == Qt::Key_S)
player.setY(player.y() + 1);
if(event->type() == Qt::Key_A)
player.setX(player.x() - 1);
if(event->type() == Qt::Key_D)
player.setX(player.x() + 1);
repaint();
}
main.cpp
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
In Window::keyPressEvent, replace event->type() with event->key() everywhere.
Also, take a look at the compiler output. I think it should have warned you about comparing QEvent::Type with Qt::Key objects.

How to detect mouse click on QLineEdit

In my QWidget there are some subwidgets like a QLineEdit and QLabels. I can easily check if my mouse is over a QLabel and if it was clicked on the right button. Not so on QLineEdit.
I tried to subclass QLineEdit and re-implement the mouseRelease, but it is never called.
The findChild method is to get the corresponding widget out off my UI.
How do I get the mouseRelease and whether it's left or right mouse button in a QLineEdit?
void Q_new_LineEdit::mouseReleaseEvent(QMouseEvent *e){
qDebug() << "found release";
QLineEdit::mouseReleaseEvent(e);
}
m_titleEdit = new Q_new_LineEdit();
m_titleEdit = findChild<QLineEdit *>("titleEdit",Qt::FindChildrenRecursively);
Clicks on labels are recognized, but the click on QLineEdit is not, like below:
void GripMenu::mouseReleaseEvent(QMouseEvent *event){
if (event->button()==Qt::RightButton){
//get click on QLineEdit
if (uiGrip->titleEdit->underMouse()){
//DO STH... But is never called
}
//change color of Label ...
if (uiGrip->col1note->underMouse()){
//DO STH...
}
}
I seem to be able to detect clicks on the line edit and distinguish which type it is in the class posted below which is very similar to what has been posted in the mentioned link
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
#include <QMouseEvent>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QtCore>
class MyClass: public QDialog
{
Q_OBJECT
public:
MyClass() :
layout(new QHBoxLayout),
lineEdit(new QLineEdit)
{
layout->addWidget(lineEdit);
this->setLayout(layout);
lineEdit->installEventFilter(this);
}
bool eventFilter(QObject* object, QEvent* event)
{
if(object == lineEdit && event->type() == QEvent::MouseButtonPress) {
QMouseEvent *k = static_cast<QMouseEvent *> (event);
if( k->button() == Qt::LeftButton ) {
qDebug() << "Left click";
} else if ( k->button() == Qt::RightButton ) {
qDebug() << "Right click";
}
}
return false;
}
private:
QHBoxLayout *layout;
QLineEdit *lineEdit;
};
#endif
main.cpp for completeness
#include "QApplication"
#include "myclass.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyClass dialog;
dialog.show();
return app.exec();
}