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();
}
Related
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();
}
I'm creatig tray icon application and i want create advanced context menu, like on pictures below, but i only know, how to create simple menues with
QMenu* menu = new QMenu()
menu->addAction(QIcon(), "item", item1Click);
trayIcon->setContextMenu(menu);
How can i do this?
Well, is supose, it's better to show you code:
main.h
#ifndef MAIN_H
#define MAIN_H
#include <QtWidgets/QApplication>
#include <QtCore/QDebug>
#include <QtGui/QIcon>
#include <QtWidgets/QSystemTrayIcon>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QWidgetAction>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QLabel>
class SpinBoxAction : public QWidgetAction
{
public:
SpinBoxAction (const QString& title) : QWidgetAction (NULL)
{
QWidget* Widget = new QWidget (NULL);
QHBoxLayout* Layout = new QHBoxLayout();
QLabel* Label = new QLabel (title);
Layout->addWidget (Label);
SpinBox = new QSpinBox(NULL);
Layout->addWidget (SpinBox);
Widget->setLayout (Layout);
setDefaultWidget(Widget);
}
QSpinBox* spinBox()
{
return SpinBox;
}
private:
QSpinBox* SpinBox;
};
class Reciever : public QObject
{
private:
QSystemTrayIcon* trayIcon;
public:
Reciever()
{
}
void setup(QSystemTrayIcon* trayIcon)
{
this->trayIcon = trayIcon;
}
Q_OBJECT
public slots:
void action(int i)
{
trayIcon->showMessage("changed", "spin box value has been changed", QSystemTrayIcon::NoIcon, 1000);
}
void onActivated(QSystemTrayIcon::ActivationReason reason)
{
trayIcon->showMessage("activated", "tray icon has been activated", QSystemTrayIcon::NoIcon, 1000);
}
};
#endif // MAIN_H
main.cpp
#include <main.h>
#include <QtWidgets/QApplication>
#include <QtCore/QDebug>
#include <QtGui/QIcon>
#include <QtWidgets/QSystemTrayIcon>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QWidgetAction>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>
int main(int argc, char** argv)
{
Reciever* reciever = new Reciever();
QApplication app(argc, argv);
QSystemTrayIcon* trayIcon = new QSystemTrayIcon(QIcon(":/images/abc.png"));
if (!trayIcon->isSystemTrayAvailable()) exit(1);
QMenu* menu = new QMenu();
SpinBoxAction* spinBoxAction = new SpinBoxAction("Action Title");
menu->addAction(spinBoxAction);
QObject::connect(spinBoxAction->spinBox(), SIGNAL(valueChanged(int)), reciever, SLOT(action(int)));
trayIcon->setContextMenu(menu);
trayIcon->setVisible(true);
QObject::connect(trayIcon, &QSystemTrayIcon::activated, reciever, &Reciever::onActivated);
reciever->setup(trayIcon);
return app.exec();
}
And it leads to simple list menu with one empty element:
I have a question with some of the code for a tutorial I'm following for Qt Creator. My first question is how is MyRect() (in file "main.cpp" on line MyRect * rect = new MyRect();) was created? I thought it was only a class and not a function. Could someone explain that?
I'm also having trouble understanding how (under "myrect.cpp") the keyPressEvent function is called whenever I press an arrow key. How does the function know to respond without me calling it? If it has been called and I've just missed it, could someone specify where, thanks.
main.cpp:
#include <QApplication>
#include <QGraphicsScene>
#include "myrect.h"
#include <QGraphicsView>
using namespace std;
int main(int argc, char *argv[]){
QApplication a(argc, argv);
// create a scene
QGraphicsScene * scene = new QGraphicsScene();
// create an item to put into the scene
MyRect * rect = new MyRect();
rect->setRect(0,0,100,100);
// add the item to the scene
scene->addItem(rect);
//Make item focusable
rect->setFlag(QGraphicsItem::ItemIsFocusable);
rect->setFocus();
// add a view to visualize the scene
QGraphicsView * view = new QGraphicsView(scene);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->show();
return a.exec();
}
myrect.cpp:
#include "MyRect.h"
#include <QGraphicsScene>
#include <QKeyEvent>
#include "bullet.h"
#include <QDebug>
void MyRect::keyPressEvent(QKeyEvent *event){
if(event->key() == Qt::Key_Left){
setPos(x()-10,y());
}
else if(event->key() == Qt::Key_Right){
setPos(x()+10,y());
}
else if(event->key() == Qt::Key_Up){
setPos(x(),y()-10);
}
else if(event->key() == Qt::Key_Down){
setPos(x(),y()+10);
}
else if(event->key() == Qt::Key_Space){
//creates a bullet
Bullet* bullet = new Bullet();
bullet->setPos(x(),y());
scene()->addItem(bullet);
}
}
myrect.h:
#ifndef MYRECT_H
#define MYRECT_H
#include <QGraphicsRectItem>
class MyRect: public QGraphicsRectItem{
public:
void keyPressEvent(QKeyEvent* event);
};
#endif // MYRECT_H
bullet.cpp:
#include "Bullet.h"
#include <QTimer>
Bullet::Bullet(){
//draw rect
setRect(0,0,10,50);
//connect
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(50);
}
void Bullet::move(){
//move bullet up
setPos(x(),y()-10);
}
bullet.h:
#ifndef BULLET_H
#define BULLET_H
#include <QGraphicsRectItem>
#include <QObject>
class Bullet: public QObject, public QGraphicsRectItem{
Q_OBJECT
public:
Bullet();
public slots:
void move();
};
#endif // BULLET_H
I'm new in Qt. I have got Class TicTacToeWidget which stores QList with QPushButton.
int m_size is initialized with 3 and works fine and i see 3x3 board, but when i try to change m_size in main.cpp to other value nothing happends. I can't find out why it doesn't work.
#ifndef TICTACTOEWIDGET_H
#define TICTACTOEWIDGET_H
#include <QWidget>
class QPushButton;
class TicTacToeWidget : public QWidget
{
Q_OBJECT
public:
TicTacToeWidget(QWidget *parent = 0);
~TicTacToeWidget();
int size()const;
void resizeBoard(int m);
private:
QList<QPushButton *> m_board;
int m_size;
void setupBoard(int m);
void clearBoard();
};
#endif // TICTACTOEWIDGET_H
And implementation
#include "tictactoewidget.h"
#include <QMessageBox>
#include <QGridLayout>
#include <QPushButton>
#include <QDebug>
TicTacToeWidget::TicTacToeWidget(QWidget *parent)
: QWidget(parent),m_size(3)
{
setupBoard(3);
}
TicTacToeWidget::~TicTacToeWidget()
{
}
int TicTacToeWidget::size() const
{
return m_size;
}
void TicTacToeWidget::resizeBoard(int m)
{
setupBoard(m);
}
void TicTacToeWidget::setupBoard(int m)
{
QGridLayout *gridLayout= new QGridLayout;
m_size=m;
m_board.clear();
for(int i=0;i<m_size;i++)
{
for(int j=0;j<m_size;j++)
{
QPushButton *button= new QPushButton;
button->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
button->setText(" ");
gridLayout->addWidget(button,i,j);
}
}
setLayout(gridLayout);
}
void TicTacToeWidget::clearBoard()
{
for(auto &it:m_board)
{
this->layout()->removeWidget(it);
}
m_board.clear();
}
And main
#include "tictactoewidget.h"
#include <QApplication>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TicTacToeWidget w;
w.resizeBoard(5);
w.show();
return a.exec();
}
http://doc.qt.io/qt-4.8/qwidget.html#setLayout
If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager
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.