Slot is not detected when QPushbutton is released [duplicate] - qt

This question already has answers here:
QObject connection function
(3 answers)
Closed 4 years ago.
I am currently making a main menu for a game, but when the button is clicked it does not seem to call the relevant slot (The button does not do anything). I have tried to move the button to a thread to ensure that nothing is keeping it from running immediately. I have written a simpler program where the button works, so it would seem the problem is not with the button or signal itself. Therefore it must be something keeping it from running?
The source file is given as:
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QObject>
#include <QBrush>
#include"game.h"
#include <QMediaPlayer>
#include <QImage>
#include<QGraphicsPixmapItem>
#include <QPixmap>
#include<QGraphicsItem>
#include <QPushButton>
#include <QLineEdit>
#include <QPalette>
#include <QMessageBox>
#include <QLabel>
#include <QComboBox>
Game::Game(QWidget*parent)
{
//adding the background
QGraphicsScene *scene=new QGraphicsScene();//to create a new graphics scene
scene->setSceneRect( 0,0,1366,768);
setBackgroundBrush(QBrush(QImage(":/images/Back_1_withlogo.jpg")));
setScene(scene);
show();
setFixedSize(1366,768);
//adding logo
//setPixmap(QPixmap(":/images/Logo.png"));
//adding the soundtrack
music=new QMediaPlayer();
music->moveToThread(&MusicThread);
music->setMedia(QUrl("qrc:/sounds/Surreal-Game-Menu.mp3"));
music->play();
MusicThread.start();
//add GUI
this->setFixedSize(1366,768);
this->move(-7,-5);
//start button and username edit box
btnStart = new QPushButton(this);
btnStart->moveToThread(&btnThread);
UserName = new QLineEdit(this);
UserName->setFixedSize(100,25);
UserName->move(300,300);
UserName->show();
UserName->setMaxLength(12);
btnStart->setText("Start");
btnStart->setFixedSize(100,30);
btnStart->show();
btnStart->move(300,600);
connect(btnStart, SIGNAL (released()),this, SLOT (btnStart_clicked()));
btnThread.start();
//label for username edit box
QLabel *LblUsername= new QLabel(this);
LblUsername->setStyleSheet("QLabel { color : white; }");
LblUsername->setFixedSize(100,30);
LblUsername->move(230,300);
LblUsername->setText("Username:");
LblUsername->show();
QLabel *lblGameMode = new QLabel(this);
lblGameMode->setStyleSheet("QLabel { color : white; }");
lblGameMode->setFixedSize(100,30);
lblGameMode->move(190,450);
lblGameMode->setText("Select Game mode:");
lblGameMode->show();
//combobox to select players
GameMode=new QComboBox(this);
GameMode->move(300,450);
GameMode->setFixedSize(100,30);
QStringList Modelist=(QStringList()<<"singleplayer"<<"co-op"<<"multiplayer (3 players)");
GameMode->addItems(Modelist);
GameMode->show();
}
void Game::btnStart_clicked()
{
if (UserName->text()==NULL)
{
QMessageBox messageBox;
messageBox.critical(0,"Error","Please insert username!");
messageBox.setFixedSize(500,200);
}
else
{
numPlayers = GameMode->currentIndex();
SUsername= UserName->text();
UserName->setText("works");
}
}
And the header code is given as:
#ifndef GAME_H
#define GAME_H
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QMediaPlayer>
#include <QThread>
#include <QComboBox>
class Game:public QGraphicsView
{
public:
Game(QWidget*parent=0);
QGraphicsScene*scene;
private slots:
void btnStart_clicked();
private:
QPushButton *btnStart;
QLineEdit *UserName;
QMediaPlayer *music;
QThread MusicThread;
QString SUsername;
QThread btnThread;
QComboBox *GameMode;
int numPlayers;
};
#endif // GAME_H
I received the following error:
QObject::connect: No such slot QGraphicsView::btnStart_clicked() in game.cpp:57

add a Q_OBJECT macro as shown below:
class Game:public QGraphicsView
{
Q_OBJECT
public:
Game(QWidget*parent=0);
QGraphicsScene*scene;
private slots:
void btnStart_clicked();
private:
QPushButton *btnStart;
QLineEdit *UserName;
QMediaPlayer *music;
QThread MusicThread;
QString SUsername;
QThread btnThread;
QComboBox *GameMode;
int numPlayers;
};
This macro helps compiler to determine that the class is using signals and slots mechanism. Basically it creates a entry of defined signals and slots in MOC (meta object compiler) file.
Go through below link for info:
http://www.bogotobogo.com/Qt/Qt5_Q_OBJECT_Macro_Meta_Object.php

QObject::connect (btnStart, &QPushButton::clicked, this, &Game::btnStart_clicked); might work... if you want to use the release event try overriding virtual void mouseRelease or the eventFilter.
And of course... the Q_OBJECT marco.

Related

Crash when adding QGraphicsProxyWidget

I'm trying to have a QPushButton in my scene, but when I'm trying to add the QGraphicsProxyWidget to the scene, it crashes.
So here's the .cpp:
#include "upgradecromagnon.h"
#include "game.h"
#include <QGraphicsProxyWidget>
#include <qDebug>
extern Game *game;
UpgradeCromagnon::UpgradeCromagnon()
{
this->setRect(-50,0,150,50);
buttonAmelio = new QPushButton("salut");
teste();
}
void UpgradeCromagnon::teste()
{
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(buttonAmelio);
scene()->addItem(proxy);
}
and its .h:
#ifndef UPGRADECROMAGNON_H
#define UPGRADECROMAGNON_H
#include <QPainter>
#include <QGraphicsRectItem>
#include <QPushButton>
class UpgradeCromagnon: public QGraphicsRectItem
{
public:
UpgradeCromagnon();
void teste();
private:
QPushButton *buttonAmelio;
};
#endif // UPGRADECROMAGNON_H
Your UpgradeCromagnon constructor calls UpgradeCromagnon::teste which, in turn, calls QGraphicsItem::scene. At that point QGraphicsItem::scene must return a null pointer since there's no possible way the UpgradeCromagnon instance can have been added to a QGraphicsScene before its contructor has completed (not according to the code you've provided at any rate).

Dynamically adding widgets to gridLayout Qt

I am having a bit of difficulty with some code. I am super rather new to Qt so it is entirely possible that I am simply ignorant to the problem I am having.
Basically, I am blocking out a program so that I can add the specifics of it later. I want to be able to create a grid of buttons, and when one of those buttons is pressed, another shape to replace it.
I am able to make my button grid, have it be scrollable, and have the button call it its position on the grid when pressed. However, when I try and use those coordinates to add another button to the grid, Qt crashes.
Here's my code so far:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath>
#include <QLabel>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QApplication>
#include <QPushButton>
#include <QScrollArea>
#include <QDebug>
#include <QString>
#include <QSignalMapper>
#include <QStringList>
#include <QLayoutItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
populateViewGrid(); //I wanted to see if I could add in a scrollbar
//from outside the main window. Could this be causing
// the issue?
}
void MainWindow::populateViewGrid()
{
QScrollArea*scrollArea = new QScrollArea(this);
QWidget*central = new QWidget(this);
QGridLayout*gridLayout = new QGridLayout(central);
QSignalMapper *signalMapper = new QSignalMapper(central);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
QString position= QString("%1,%2").arg(i).arg(j);
QPushButton* button = new QPushButton("addTrack",central);
gridLayout->addWidget(button, i, j);
connect(button, SIGNAL(clicked()),signalMapper, SLOT(map()));
signalMapper->setMapping(button, position);
}
}
connect(signalMapper, SIGNAL(mapped(QString)),this, SLOT(addTrack(QString )));
central->setLayout(gridLayout);
scrollArea->setWidget(central);
setCentralWidget(scrollArea);
}
void MainWindow::addTrack(QString position)
{
QStringList query = position.split(",");
int x;
x=query.at(0).toInt();
int y;
y=query.at(1).toInt() ;
QPushButton *Ifthisworks=new QPushButton(this);
//This first line is where is crashes. I know this due to having the code
//laced with qDebugs. From all of my google searches and such, it seems that
// something simple should be wrong and I can't find it.
QLayoutItem * existingitem = gridLayout->itemAtPosition(x, y);
if(existingitem) {
gridLayout->removeItem(existingitem);
delete existingitem;
}
// before I included the above to remove the button from the grid point, the
//program would crash here.
gridLayout->addWidget(Ifthisworks, x, y);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <cmath>
#include <QLabel>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QApplication>
#include <QPushButton>
#include <QMainWindow>
#include <QScrollArea>
#include <QSignalMapper>
#include <QHash>
//unrelated question, do I need the above in my code? I know not all of them
//used, but do I need includes in this file as well?
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void populateViewGrid();
QGridLayout *gridLayout;
public slots:
void addTrack(QString);
private:
QScrollArea*scrollArea;
QWidget * central;
QPushButton *Ifthisworks;
QSignalMapper *signalMapper;
QPushButton *clockViews;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
If you could help me understand how to not make Qt crash there and also add a button, that would be fantastic =)
So some background real quick incase you are looking at my code and are scratching your head. I'm a mechanical engineer who should have probably been an electrical or computer engineer and knows enough about coding to get myself into these kinds of messes. For the most part, I searched for what I wanted Qt to do and hacked it all together to hopefully make it work. Any bit of deeper understanding you can share would be more than welcome.
Thank you for your time.
You are initializing a local variable called gridLayout in your MainWindow::populateViewGrid() method:
QGridLayout*gridLayout = new QGridLayout(central);
Then in your MainWindow::addTrack(QString position) method, you are trying to access the member variable called gridLayout which is never initialized.
To fix this, simply initialize the member variable instead of creating a local variable in your MainWindow::populateViewGrid() method:
gridLayout = new QGridLayout(central);
You are doing the same mistake with your other member variables as well. Fix them the same way.
I would go for a different implementation.
Move gridlayout, signalmapper,... to be class members. I personally like also to keep a list of my widgets QList<QPushButton*> for manually deleting or keeping a button cache.
Make the signalmapper to map to index in list or QWidget*. For the example i`ll go with the QWidget* pointer.
QPushButton *newButton = new QPushButton("The new Thing");
QPushButton *oldButton = static_cast<QPushButton*>(widgetPointer);
gridLayout->replaceWidget(oldButton ,newButton);
buttonList->takeAt(buttonList->indexOf(oldButton))->deleteLater()); //if you keep a list..
buttonList->insert(buttonList->indexOf(oldButton),newButton);

Connecting buttons to mainwindows slot

I tried to do a bunch of research on how to solve this problem, and everything is slightly different than my situation, or didn't work to fix my problem. I will start off by explaining my main goal. I have a main window with 7 buttons on it(amongst other things), when you hit each button, it closes out the current window and opens up a new window. All the windows will have the same 7 buttons, so you can go between each window. With all windows having the exact same 7 buttons, I wanted to set up a function that each class can call to set up each button and connect to a slot() in my mainwindow.cpp(called setupSubsystemButtons in example below). The actual buttons are being placed there, but they only work when pressed from my mainwindow.cpp....when I press them from a different class nothing happens.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWidgets>
#include <QDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
QWidget *window;
void setupSubsystemButtons(QGridLayout *layout);
~MainWindow();
private:
Ui::MainWindow *ui;
QLineEdit *tempValueBox;
QLineEdit *humidityValueBox;
QLineEdit *c02ValueBox;
...
public slots:
void ECSgeneralScreen();
void homeScreen();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ecsgeneralcommand.h"
#include <QtWidgets>
#include <QtCore>
MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
{
QGridLayout *layout = new QGridLayout;
...
setLayout(layout);
}
void MainWindow::ECSgeneralScreen()
{
ECSgeneralCommand *ECSgeneral = new ECSgeneralCommand;
this->close();
ECSgeneral->show();
//opens up the ECS screen
}
void MainWindow::homeScreen()
{
MainWindow *home = new MainWindow;
this->close();
home->show();
//opens up the home screen
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setupSubsystemButtons(QGridLayout *layout)
{
//Push Button Layout
homeScreenButton = new QPushButton("Home");
layout->addWidget(homeScreenButton, 3, 11);
connect(homeScreenButton, SIGNAL(clicked()), this, SLOT(homeScreen()));
ECSgeneralScreenButton = new QPushButton("General");
layout->addWidget(ECSgeneralScreenButton,5,11);
connect(ECSgeneralScreenButton, SIGNAL(clicked()), this, SLOT(ECSgeneralScreen()));
}
ecsgeneralcommand.h
#ifndef ECSGENERALCOMMAND_H
#define ECSGENERALCOMMAND_H
#include <QDialog>
#include <QMainWindow>
#include <QtWidgets>
#include <QObject>
#include "mainwindow.h"
class ECSgeneralCommand : public QDialog
{
Q_OBJECT
public:
explicit ECSgeneralCommand(MainWindow *parent = 0);
private:
...
public slots:
};
#endif // ECSGENERALCOMMAND_H
ecsgeneralcommand.cpp
#include "ecsgeneralcommand.h"
#include "mainwindow.h"
#include <QtWidgets>
#include <QtCore>
ECSgeneralCommand::ECSgeneralCommand(MainWindow *parent) : QDialog(parent)
{
QGridLayout *layout = new QGridLayout;
QWidget::setFixedHeight(600);
QWidget::setFixedWidth(550);
...
MainWindow setupButtons;
//Setup Subsystem Buttons
setupButtons.setupSubsystemButtons(layout);
setLayout(layout);
};
MainWindow setupButtons;
//Setup Subsystem Buttons
setupButtons.setupSubsystemButtons(layout);
This will create the buttons and connect their signals to slots of setupButtons, which will get deleted as soon as it's out of scope (the end of the ECSgeneralCommand constructor). So your buttons will be left connected to nothing.
You need to connect the button signals to an object that will exist at the time the button is pressed, such as the ECSgeneralCommand itself. Then it could close itself and spawn the correct window.
Or, possibly a much better solution, if applicable for your application: Use a single main window, with a QStackedWidget that switches widgets when a button is pressed. That's what's typically done.

SLOT rejected(), accepted() in QMainWindow

I am a newbie in Qt-programming. I have read a book about GUI-programming with Qt. I have an trouble in creating a dialog. Here is sample code:
// gotocell.h
#ifndef GOTOCELL_H
#define GOTOCELL_H
#include <QDialog>
#include <QtWidgets>
#include "ui_gotocell.h"
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
Q_OBJECT
public:
GoToCellDialog (QWidget *parent = 0);
private slots:
void on_lineEdit_textChanged();
};
#endif // GOTOCELL_H
// gotocell.cpp
#include <QtWidgets>
#include "gotocell.h"
#include <QtWidgets>
GoToCellDialog::GoToCellDialog (QWidget *parent):
QDialog (parent)
{
setupUi(this);
QRegExp regExp ("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new QRegExpValidator(regExp, this));
connect (okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect (cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
// main.cpp
#include "gotocell.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GoToCellDialog *dialog = new GoToCellDialog;
dialog->show();
return a.exec();
}
but when I compiled, there is an error: no known conversion for argument 1 from 'GoToCellDialog* const' to 'QMainWindow*'at setupUi() function. I think because the designer in Qt Creator created a QMainWindow, not a QDialog. So I changed GoToCellDialog class to QMainWindow. But there is no slots whose name is "accepted", "rejected" in QMainWindow. Can anyone help me?
If you want to display a Dialog as main window you have two choices:
1. make the whole main window QDialog based
2. design the Dialog separately and set it as the main windows central Widget (QMainWindow->setCentralWidget()).
In both cases you still have the problem what semantics you give to the OK and Cancel buttons. Generally it may be a better idea to consider what the main window of the application should contain, and design the dialogs later.

create custom Qwidget in Qt?

I create sample application in that I was used first two Qwidget from UI form and third widget is custom one. I created one cpp file and header file. there is no issues when build while run the application the first two widget come fine and when i click the to navigate third one, it say's error( login.exe file has stop working)
My header file is:
#ifndef LISTWIDGET_H
#define LISTWIDGET_H
#include <QObject>
#include <QWidget>
#include <QtGui>
#include <QPushButton>
class listWidget : public QWidget
{
Q_OBJECT
public:
explicit listWidget(QWidget *parent=0);
~listWidget();
public:
QPushButton *button;
signals:
};
#endif // LISTWIDGET_H
and my cpp file is:
#include "listwidget.h"
#include <QHBoxLayout>
#include <QObject>
#include <QWidget>
#include <QtGui>
listWidget::listWidget(QWidget *parent):QWidget(parent)
{
resize(100,100);
button = new QPushButton("Click here to go back");
QHBoxLayout *hLayout;
hLayout->addWidget(button);
setLayout(hLayout);
}
listWidget::~listWidget()
{
}
Here is your problem:
QHBoxLayout *hLayout;
hLayout->addWidget(button);
You forgot to either:
instantiate and assign on object for hLayout to point to:
hLayout = new QHBoxLayout();
or instantiate hLayout on the spot:
QHBoxLayout hLayout;
hLayout.addWidget(button);
Basically you are dereferencing an uninitialized pointer and in most cases your application would crash.

Resources