I let Qt-Creator to generate me a basic window app using Qt. I added there a button and try to connect it to a slot. It compiles fine but the app crashes and returns 3, please help me, I don't know what to do.
#include "form1.h"
#include "ui_form1.h"
#include <iostream>
Form1::Form1(QWidget *parent) : QMainWindow(parent), ui(new Ui::Form1)
{
connect(ui->buttonLoad,&QPushButton::clicked,this,&Form1::ButtonLoadClick);
ui->setupUi(this);
}
Form1::~Form1()
{
delete ui;
}
void Form1::ButtonLoadClick(){
using namespace std;
cout << "click!" << endl;
}
In the header I only added one public slot.
#ifndef FORM1_H
#define FORM1_H
#include <QMainWindow>
namespace Ui {
class Form1;
}
class Form1 : public QMainWindow
{
Q_OBJECT
public:
explicit Form1(QWidget *parent = 0);
~Form1();
public slots: //here
void ButtonLoadClick(); //here
private:
Ui::Form1 *ui;
};
#endif // FORM1_H
Try:
Form1::Form1(QWidget *parent) : QMainWindow(parent), ui(new Ui::Form1)
{
ui->setupUi(this);
connect(ui->buttonLoad,&QPushButton::clicked,this,&Form1::ButtonLoadClick);
}
setupUi creates all visual components, including push button.
Related
I will be glad if you could help me a little.
So as it is in topic I have error trying to do Q_OBJECT::connect.
So my code is:
preferences.h:
#include <QDialog>
#include <QApplication>
#include <QCoreApplication>
namespace Ui {
class Preferences;
}
class Preferences : public QDialog
{
Q_OBJECT
public:
explicit Preferences(QWidget *parent = 0);
~Preferences();
private:
Ui::Preferences *ui;
};
preferences.cpp:
#include "preferences.h"
#include "ui_preferences.h"
Preferences::Preferences(QWidget *parent) :
QDialog(parent),
ui(new Ui::Preferences)
{
ui->setupUi(this);
}
Preferences::~Preferences()
{
delete ui;
}
mainwindow.h:
#include "preferences.h"
#include "addkierowca.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Preferences *PreferencesWindow;
private:
/* some private methods */
void showPreferencesWindow();
And last but not least mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionUstawiania, SIGNAL(triggered()), this, SLOT(showPreferencesWindow()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showPreferencesWindow()
{
PreferencesWindow = new Preferences(this);
PreferencesWindow->show();
PreferencesWindow->exec();
}
I couldn't found anwser anywhere, and I know that there are almost same topics, but none of them helped me.
Thanks in advice.
You are using showPreferencesWindow as a slot in the connect so need to at it to a slot.
change to:
private slots:
void showPreferencesWindow();
At that point, moc (used by the makefile) will generate the correct code needed for connect to work correctly.
Welcome, I have a question about Qt's windows operations. I have a simple app which contains 2 windows:
MainWindow - includes push button, can't be resizeable,
AdminWindow - includes label, can be resizeable.
When I click push button, it should open AdminWindow and hide MainWindow. I made the app and it seems to work but when I open the AdminWindow, the application icon which is located in windows's bottom bar is missing. How can I fix it?
Icon is showed when MainWindow is opened:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "adminwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtonOpenAdmin_clicked();
private:
Ui::MainWindow *ui;
AdminWindow *adminWindow;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// hides MainWindow and show AdminWindow
void MainWindow::on_pushButtonOpenAdmin_clicked()
{
hide();
adminWindow = new AdminWindow(this);
adminWindow->show();
}
adminwindow.h
#ifndef ADMINWINDOW_H
#define ADMINWINDOW_H
#include <QMainWindow>
namespace Ui {
class AdminWindow;
}
class AdminWindow : public QMainWindow
{
Q_OBJECT
public:
explicit AdminWindow(QWidget *parent = 0);
~AdminWindow();
private:
Ui::AdminWindow *ui;
};
#endif // ADMINWINDOW_H
adminwindow.cpp
#include "adminwindow.h"
#include "ui_adminwindow.h"
AdminWindow::AdminWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AdminWindow)
{
ui->setupUi(this);
}
AdminWindow::~AdminWindow()
{
delete ui;
}
I've set up a basic Qt-Widgets-Application (Qt 5.5 community) with a simple QWidget "MainWindow" and an additinal QWidget "SettingsScreen".
Within the "MainWindow", touchscreen-events (handled by OS) are working as expected, but after opening the "SettingsScreen" all touch-events are executed on the desktop until I close the "SettingsScreen" using mouse or keyboard.
Environment:
Ubuntu Studio 14.04.03
Qt 5.5 Open Source Edition
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <settingsscreen.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btnExit_clicked();
void on_btnSettings_clicked();
private:
Ui::MainWindow *ui;
SettingsScreen *wSettingsScreen;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settingsscreen.h"
#include "ui_settingsscreen.h"
MainWindow::MainWindow(QWidget *parent) : QWidget(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnExit_clicked()
{
this->close();
}
void MainWindow::on_btnSettings_clicked()
{
wSettingsScreen = new SettingsScreen(parentWidget());
wSettingsScreen->show();
}
settingsscreen.h
#ifndef SETTINGSSCREEN_H
#define SETTINGSSCREEN_H
#include <QWidget>
namespace Ui {
class SettingsScreen;
}
class SettingsScreen : public QWidget
{
Q_OBJECT
public:
explicit SettingsScreen(QWidget *parent = 0);
~SettingsScreen();
private slots:
void on_pushButton_clicked();
void on_btnBack_clicked();
private:
Ui::SettingsScreen *ui;
};
#endif // SETTINGSSCREEN_H
settingsscreen.cpp
#include "settingsscreen.h"
#include "ui_settingsscreen.h"
SettingsScreen::SettingsScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::SettingsScreen)
{
ui->setupUi(this);
}
SettingsScreen::~SettingsScreen()
{
delete ui;
}
void SettingsScreen::on_btnBack_clicked()
{
this->close();
}
I've just started developing with Qt, so please forgive me if I'm missing something essential :)
Any help would by highly appreciated!!
Thank you in advance!
Actually the thing is QMainwindow or any base Widget of your application is able to properly synthesize the Unhandled touch screen events to Mouse events. So whenever you are creating a dialog/widget, make sure to set Mainwindow as there parent and in the constructor of the child widget use setParent(parent). Even I was facing this kind of Issue and this worked for me.
I am currently learning Qt and I seem to have run into a problem.
In my practice project I have 2 classes: MainWindow and Dialog.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtonDialog_clicked();
private:
Ui::MainWindow *ui;
Dialog *dialogInstance;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dialogInstance = new Dialog(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtonDialog_clicked()
{
dialogInstance->show();
}
Dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
Dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
My goal is to input a value using the Dialog window, then have the value of that input shown on the MainWindow, I know how to pass variables around within the class using widgets, but I am not sure how to transfer variables between unrelated objects.
Any input would be of great help.
Try this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButtonDialog, SIGNAL(clicked()), this, SLOT(on_pushButtonDialog_clicked()));
dialogInstance = new Dialog(this);
}
// ...
I made a program (a very beginner type program). When i compile it, the IDE keeps on Setting Break and never runs the program. I have checked the program, for me it has no errors. Any suggestions what to do.
Here is the Bobwindow.cpp
#include "bobwindow.h"
#include "ui_bobwindow.h"
#include "askdialog.h"
#include <QListWidgetItem>
BobWindow::BobWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::BobWindow)
{
ui->setupUi(this);
dialog= new AskDialog(this);
item= new QListWidget(this);
}
BobWindow::~BobWindow()
{
delete ui;
}
void BobWindow::on_actionAdd_Item_triggered()
{
dialog->show();
}
void BobWindow::showlist(QString &data)
{
QListWidgetItem *itm= new QListWidgetItem;
QFont fnt;
fnt.setFamily("Times");
fnt.setPointSize(18);
fnt.setItalic(true);
itm->setText(data);
itm->setSizeHint(QSize(0,25));
itm->setFont(fnt);
itm->setBackground(Qt::green);
itm->setIcon(QIcon(":/pics/goku3.jpg"));
item->addItem(itm);
item->setIconSize(QSize(15,15));
}
here is the Askdialog.cpp file
#include "bobwindow.h"
#include "askdialog.h"
#include "ui_askdialog.h"
AskDialog::AskDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AskDialog)
{
ui->setupUi(this);
prog= new BobWindow;
}
AskDialog::~AskDialog()
{
delete ui;
}
void AskDialog::on_dokbutton_clicked()
{
ui->dokbutton->setEnabled(false);
ui->dokbutton->setDefault(true);
QString data=ui->dline->text();
prog->showlist(data);
}
here is askdialog.h file
#ifndef ASKDIALOG_H
#define ASKDIALOG_H
#include <QDialog>
class BobWindow;
namespace Ui {
class AskDialog;
}
class AskDialog : public QDialog
{
Q_OBJECT
public:
explicit AskDialog(QWidget *parent = 0);
void showdialog();
~AskDialog();
private slots:
void on_dokbutton_clicked();
private:
Ui::AskDialog *ui;
BobWindow *prog;
};
#endif // ASKDIALOG_H
here is Bobwindow.h file
#ifndef BOBWINDOW_H
#define BOBWINDOW_H
#include <QMainWindow>
#include <QListWidget>
#include "askdialog.h"
namespace Ui {
class BobWindow;
}
class BobWindow : public QMainWindow
{
Q_OBJECT
public:
explicit BobWindow(QWidget *parent = 0);
~BobWindow();
void showlist(QString &);
private slots:
void on_actionAdd_Item_triggered();
private:
Ui::BobWindow *ui;
QListWidget *item;
AskDialog *dialog;
};
#endif // BOBWINDOW_H
what should i do to fix it?
Everything's fine.
It breaks on the breakpoint because you have set breakpoints and running the program in debug mode (for sure).
Are there any breakpoints set by you? If so, remove them and run in debug mode.
Else, you can run the program in release mode.
To run the program in release mode use: Ctrl+R shortcut key
More on shortcuts here: http://doc.qt.nokia.com/qtcreator-2.4/creator-keyboard-shortcuts.html