Qt Message Box Won´t appear [closed] - qt

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i recently started working with qt and there occurs a problem i do not understand:
In this particular code i just want to show a Message Box, if a push button is clicked and a radio button is checked, but the Message Box wont appear. Can anyone help?
#include "thirddialog.h"
#include "ui_thirddialog.h"
#include <QMessageBox>
ThirdDialog::ThirdDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ThirdDialog)
{
ui->setupUi(this);
this->setWindowTitle("Eingabe UWZ"); /*Titelzeile*/
}
ThirdDialog::~ThirdDialog()
{
delete ui;
}
void ThirdDialog::on_Back_clicked()
{
close();
}
void ThirdDialog::on_pushButton_clicked()
{
if(ui->radioButton->isChecked()) {
QMessageBox::information(this,"Title","1");
}
if(ui->radioButton_2->isChecked()) {
QMessageBox::information(this,"Title","2");
}
}

Seems to me like you need to connect your on_pushButton_clicked() with the clicked() signal of the QPushButton.
connect(ui->pushButton, &QPushButton::clicked, this, &ThirdDialog::on_pushButton_clicked);
For this to work on_pushButton_clicked() must be a slot:
class ThirdDialog {
Q_OBJECT
public:
...
public slots:
void on_pushButton_clicked();
private:
...
};

Related

Qt - QLineEdit does not emit textChanged/textEdited Signal while QTextEdit does

Currently i am learning Qt in C++ and have following issue:
Connecting "textChanged" or "textEdited" with a slot does not work.
Header file:
#include ...
class PersonalPreferences : public QWidget
{
Q_OBJECT
public:
PersonalPreferences(QWidget *parent = nullptr);
public slots:
void make_available();
private:
...
};
Code file:
PersonalPreferences::PersonalPreferences(QWidget *parent) {
// Configuring Layout
connect(line_edit_name, SIGNAL(textEdited()), this, SLOT(make_available()));
}
make_available function:
void PersonalPreferences::make_available() {
label_name->setText("wadwad");
group_box_pref->setEnabled(true);
push_button_ok->setEnabled(true);
push_button_close->setEnabled(true);
}
Please notice that i intentionally reduced the code to it's significant parts and didn't include the main file. I tried the same code with and without the parameter make_available(const QString &text). Yet it does not change the outcome.
I also tried this
connect(line_edit_name, &QLineEdit::textEdited, this, &PersonalPreferences::make_available);
and still it did not work out.
I noticed that using QTextEdit instead of QLineEdit works, but nevertheless i need help to solve this issue with QLineEdit.
Thanks in advance!
you need to study the details carefully)
In QLineEdit, the signal is
void textChanged ()
In QTextEdit, the signal is
void textChanged (const QString & text)
then you need to make such an amendment
void make_available()
change to
void make_available(const QString & text)
and call
connect(line_edit_name, &QLineEdit::textEdited, this, &PersonalPreferences::make_available);
or
connect(line_edit_name, SIGNAL(textEdited()), this, SLOT(make_available()));
change to
connect(line_edit_name, SIGNAL(textEdited(QString)), this, SLOT(make_available(QString)));

QGraphicsSceneMouseEvent get position will not work [duplicate]

This question already has an answer here:
Qt Widgets and derived classes
(1 answer)
Closed 3 years ago.
I am trying to make an application with a QGraphicsView in it. I tried to get the position of a mouse when the mouse was pressed with QGraphicsSceneMouseEvent, but it doesn't seem to work. The entire function is never called when I press the mouse.
I would like to make an application that can load an image into the QGraphicsView and then when you press the mouse button, it should add a small circle.
This is my code:
dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->graphicsView = new GraphicsView();
}
Dialog::~Dialog()
{
delete ui;
}
graphicsview.h:
#ifndef GRAPHICSVIEW_H
#define GRAPHICSVIEW_H
#include <QGraphicsView>
#include <QMouseEvent>
#include <QDebug>
class GraphicsView : public QGraphicsView
{
public:
GraphicsView();
protected:
void mousePressEvent(QMouseEvent *event) override;
private:
QGraphicsScene *scene;
};
#endif // GRAPHICSVIEW_H
graphicsview.cpp:
#include "graphicsview.h"
GraphicsView::GraphicsView()
{
scene = new QGraphicsScene();
this->setScene(scene);
}
void GraphicsView::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
qDebug() << "The left button was pressed!";
}
}
You're almost there.
Just change GraphicsView::GraphicsView() to:
GraphicsView::GraphicsView(QWidget *parent=nullptr) : QGraphicsView(parent) {}
and change ui->graphicsView = new GraphicsView(); to
QVBoxLayout *verticalLayout = new QVBoxLayout(this);
verticalLayout->addWidget(new GraphicsView(this));
This will make the graphics view a parent of the dialog. Make sure that your dialog doesn't have anything in it when you add the code above. Alternatively, you can add a vertical layout to the dialog in your dialog.ui file and do
ui->verticalLayout->addWidget(new GraphicsView(this));
I believe that solves your problem. Respond if you still have any issues.
Make sure you always have a parent parameter. Without it, you're not telling your program where it's supposed to add your widget.

QT push button / stop button

I have a push button that I'd like to change to a stop button when clicked. Currently the button's text says "auto fire", it runs an endless loop and it's text changes to "stop auto fire" when clicked. My problem is breaking the endless loop by clicking/pressing this button again after the text changes.
Code so far:
void Cpp_Fire::on_auto_fire_clicked()
{
while(true)
{
ui->auto_fire->setText("Stop Auto Fire");
on_manual_fire_clicked();
}
}
I tried inserting a different slot into the loop above that runs when after the button is pressed (it runs when the button is released to be precise) but I couldn't get it to work.
I know this could be done with signals/slots and a separate stop button but I'm unfamiliar with that method and I prefer the method I described.
The problem with your endless loop is that nothing else gets a chance to work.
One approach you could use is to use a QTimer with a short interval to call the on_manual_fire_clicked() method, then have the on_auto_fire_clicked() method be responsible for changing the text on the button and enabling / disabling the timer.
The ui should get enough time to respond to clicks etc if you do it that way.
edit:
For more info on using QTimer have a look at this page:
How to use QTimer
or this tutorial:
http://www.bogotobogo.com/Qt/Qt5_QTimer.php
Here's some code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void timerslot();
private:
Ui::MainWindow *ui;
QTimer* myTimer;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTimer>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myTimer = new QTimer(this);
myTimer->setInterval(500);
myTimer->setSingleShot(false);
connect(myTimer, SIGNAL(timeout()), this, SLOT(timerslot()));
myTimer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerslot()
{
qDebug() << "timeslot";
}
void MainWindow::on_pushButton_clicked()
{
if ( this->myTimer->isActive() == true ) {
this->myTimer->stop();
ui->pushButton->setText("Start");
} else {
this->myTimer->start(500);
ui->pushButton->setText("Stop");
}
}
I hope you get the idea and can convert it to your needs.
I fully agree Michael's answer.
This will also affect the repaint! (Try to put some windows over your application, while in endless-loop: you should see repaint problems).
Don't use endless-loops, specially not within slots!
Try QTimer, or move object to a QThread.
While in such loop: Give GUI-Thread some time. You can call QCoreApplication::processEvents().. But be careful with this.
A simple (still poor) solution with QTimer could be:
(I found, Michael entered an example in his answer. - Use it.).
//have a QTimer 'timer' in the class, and a connect signal
//timer.timeout() to 'onSingleShotFired()'
void Cpp_Fire::on_auto_fire_clicked()
{
if ( ui->auto_fire->text() == "Stop Auto Fire" )
{
timer.stop();
ui->auto_fire->setText("Start Auto Fire");
}
else
{
//MSEC_AUTOFIRE_DELAY is the delay between the autofire-shots
timer.start( MSEC_AUTOFIRE_DELAY );
ui->auto_fire->setText("Stop Auto Fire");
}
}

Qt - drawing after receiving signal

I have a simple application, that consists of one line edit, where user writes an answer and a confirmation button. If the answer was correct, I'd like to draw a simple figure (green / red circle for example). I have three source files - main, form (here I connect the signal from button with answer from lineedit and determine if answer is correct) and figureWidget, where i'd like to draw my figure. My problem is, that I can't find out how to connect the signal from "form" with my figureWidget (especially what with what and where :-) ) Any hints?
file form.cpp:
#include <QtCore>
#include "ui_untitled.h"
#include "Form.h"
#include "figureWidget.h"
Form::Form(QWidget * parent, Qt::WindowFlags f) : QWidget ( parent,f ) {
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(buttonPressed()), this, SLOT(checkAnswer()))
//this checks if answer is correct after pushing button
connect(this,SIGNAL(correctness(QString)),ui.figureWidget, SLOT(drawFigure(QString)));
//I expect signal from this class (here I check the answer), and draw the circle in figureWidget
}
void Form::checkAnswer() {
if (ui.lineEdit == "1") emit correctness(QString("right");
else emit correctness(QString("false")
}
then I have the figureWidget.cpp:
#include <QtGui>
#include <iostream>
#include "figureWidget.h"
figureWidget::figureWidget(QWidget* parent) : QWidget(parent) {
}
figureWidget::~figureWidget() {
}
void figureWidget::drawFigure(QString newVal) {
if (newVal == correctVal)
update();
}
void figureWidget::paintEvent(QPaintEvent* ) {
QPainter painter(this);
painter.setPen(QRgb(0x0000ff00));
painter.drawEllipse(0, 0, 10, 10);
}
in debug i get:
No such slot QWidget::drawFigure(QString) in ...
I think you can try this:
connect(this,SIGNAL(correctness(QString)),this, SLOT(drawFigure(QString)));
void Form::drawFigure(QString val)
{
ui->figureWidget->drawFigure(val);
}
But how could figureWidget appears after 'ui->'??? You cannot draw it on the UI statically.

Qt designer adding a link to a QPushButton

I have a QPushButton in QT that I need to have redirect to a website, currently the button does not go anywhere when I inherited it. I have never worked with QT before so am looking for what would need to be done to make this button redirect to a URL when hit.
Right now the properties for it have
QObject
QWidget
QAbstractButton
QPushButton
Please help :)
You can extend QPushButon:
#include <QDesktopServices>
#include <QUrl>
#include "LinkButton.h"
LinkButton::LinkButton(QWidget *parent) : QPushButton(parent) {
this->setFlat(true);
connect(this, SIGNAL(clicked()), this, SLOT(slotOpenUrl()));
}
void LinkButton::setUrl(QString url) {
this->url = url;
}
void LinkButton::slotOpenUrl() {
QDesktopServices::openUrl(QUrl(this->url));
}

Resources