Header is not automatically generated by wizard in qt-creator - c++

today I wanted to start diving in into QT by doing the official notepad tutorial. It is said that the header will be generated automatically to this:
#include <QMainWindow>
namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow
{
Q_OBJECT
public:
explicit Notepad(QWidget *parent = 0);
~Notepad();
private slots:
void on_actionNew_triggered();
void on_actionOpen_triggered();
void on_actionSave_triggered();
void on_actionSave_as_triggered();
void on_actionPrint_triggered();
void on_actionExit_triggered();
void on_actionCopy_triggered();
void on_actionCut_triggered();
void on_actionPaste_triggered();
void on_actionUndo_triggered();
void on_actionRedo_triggered();
void on_actionFont_triggered();
void on_actionBold_triggered();
void on_actionUnderline_triggered();
void on_actionItalic_triggered();
void on_actionAbout_triggered();
private:
Ui::Notepad *ui;
QString currentFile;
};
But after saving and even restartin qtcreator I'm getting this:
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow
{
Q_OBJECT
public:
explicit Notepad(QWidget *parent = nullptr);
~Notepad();
private:
Ui::Notepad *ui;
};
#endif // NOTEPAD_H
Is there any information file about configuration I could provide to allow you to help me? Or anything in the settings?
As far as I can see there is some problem with the Design .ui tool.

Related

Qt: out-of-line definition of "closeEvent" does not match any declaration in "MainWindow"

I just want to ignore the closing event thrown by the user in Qt, C++
I tried what is already in the docs of Qt
void MainWindow::closeEvent(QCloseEvent *event)
{
event->ignore();
}
But this throws me the error out-of-line definition of "closeEvent" does not match any declaration in "MainWindow". I'd expect to ignore the closing event.
The header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
-
#endif // MAINWINDOW_H
The solution is that you need to declare in the header file that you will override the closeEvent. In the docs you can se that is virtual protected, that means that you can override it.
Your code should be the following:
The header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
void closeEvent(QCloseEvent *event) override;
private:
Ui::MainWindow *ui;
};
-
#endif // MAINWINDOW_H
The cpp file:
void MainWindow::closeEvent(QCloseEvent *event)
{
event->ignore();
}

setWindowState from QMainWindow from QWidget

I have a QMainWindow Application which also includes an QStackedWidget.
The pages of the QstackedWidget are promoted to ui widgets for example heating_widget.ui
If I use a button slot on my QMainWindow I can use this to get my application to fullscreen:
void SmartHome::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
But how can I do this from a button which is in the heating_widget.cpp file?
Using:
void heating_widget::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
obviously doesn't work and throws this error at me:
cannot call member function 'void
QWidget::setWindowState(Qt::WindowStates)' without object
SmartHome::setWindowState(Qt::WindowFullScreen);
I know this has something to do with parent() but I can't get it to work.
Do you have any idea?
My smarthome.h file:
#ifndef SMARTHOME_H
#define SMARTHOME_H
#include <QTime>
#include <QMainWindow>
namespace Ui {
class SmartHome;
}
class SmartHome : public QMainWindow
{
Q_OBJECT
public:
explicit SmartHome(QWidget *parent = 0);
~SmartHome();
private slots:
void on_Info_Button_clicked();
void on_News_Button_clicked();
void on_Heating_clicked();
void timerslot();
void on_Config_clicked();
void on_About_clicked();
public slots:
void setFullscreen();
private:
Ui::SmartHome *ui;
QTimer* myTimer;
};
#endif // SMARTHOME_H
My heating_widget.h :
#ifndef HEATING_WIDGET_H
#define HEATING_WIDGET_H
#include "smarthome.h"
#include <QWidget>
namespace Ui {
class heating_widget;
class SmartHome;
}
class heating_widget : public QWidget
{
Q_OBJECT
public:
explicit heating_widget(QWidget *parent = 0);
~heating_widget();
private slots:
void on_fullscreen_on_clicked();
private:
Ui::heating_widget *ui;
};
#endif // HEATING_WIDGET_H
and my heating.widget.cpp:
#include "heating_widget.h"
#include "ui_heating_widget.h"
#include "smarthome.h"
#include "iostream"
heating_widget::heating_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::heating_widget)
{
ui->setupUi(this);
QObject::connect(ui->fullscreen_on, SIGNAL(clicked()), this , SLOT(SmartHome::setFullscreen()));
}
heating_widget::~heating_widget()
{
delete ui;
}
void heating_widget::on_fullscreen_on_clicked()
{
parentWidget()->setWindowState(Qt::WindowFullScreen);
std::cout<<"clicked"<<std::endl;
}
I would do it in the following way:
void heating_widget::on_fullscreen_on_clicked()
{
foreach(QWidget *widget, QApplication::topLevelWidgets()) {
if (auto mainWindow = qobject_cast<SmartHome *>(widget)) {
mainWindow->setWindowState(Qt::WindowFullScreen);
}
}
}
The idea is finding your main window among application top level widgets and change its state. This code can be called from anywhere in your application regardless of the windows hierarchy.

missing ';' before '*' after instance declaration

I've encountered an error which I cannot seem to find a solution to anywhere else.
The error occurs when I'm trying to declare an instance of the "EncodeWindow" class.The compiler is giving errors C2143,C4430 and C2238. I am simply trying to give the "MainWindow" class an instance of "EncodeWindow".
File mainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QLabel>
#include "Image.h"
#include "encodewindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
/*Check whether a given file path is valid*/
bool CheckFilePath(QString);
/*Sets UI widgets*/
void setOriginalImage_Label(const char*);
void setEncodedImage_Label(const char*);
void setDebug_TextBox(QString);
/*Saves all information about current encoding/decoding*/
void saveFile();
private slots:
void on_Encode_Button_clicked();
void on_Reset_Button_clicked();
void on_Save_Button_clicked();
void on_AddEncodeImage_Debug_Button_clicked();
void on_AddImage_Button_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
EncodeWindow *CurrentEncodeWindow = new EncodeWindow; //ERROR HERE!! C2143
int fileNumber = 0;
};
#endif // MAINWINDOW_H
File encodeWindow.h:
#ifndef ENCODEWINDOW_H
#define ENCODEWINDOW_H
#include <QMainWindow>
#include "mainwindow.h"
namespace Ui {
class EncodeWindow;
}
class EncodeWindow : public QMainWindow
{
Q_OBJECT
public:
explicit EncodeWindow(QWidget *parent = 0);
~EncodeWindow();
/*Get filepaths from 'result' object (For access to filepaths without 'encode' Window)*/
const char* getOriginalFilePath();
const char* getEncodeFilePath();
const char* getSaveFilePath();
bool checkUI(const char*,const char*,const char*,bool*);
bool CheckFilePath(const char*);
std::string readInText(const char*);
private slots:
void on_RedChannel_CheckBox_clicked(bool);
void on_GreenChannel_CheckBox_clicked(bool);
void on_BlueChannel_CheckBox_clicked(bool);
void on_AlphaChannel_CheckBox_clicked(bool);
void on_BitDepth_Slider_sliderMoved(int);
void on_BitDepth_SpinBox_valueChanged(int);
void on_Encode_Button_clicked();
private:
Ui::EncodeWindow *ui;
Encoded result; //Enocoded object (child of Image class)
};
#endif // ENCODEWINDOW_H
Any help would be great. The code was done in Qt for an image steganography project.
Thanks.
You have circular includes. "mainwindow.h" includes "encodewindow.h" and "encodewindow.h" includes "mainwindow.h". The include guards stop the cycle, but the class definitions for whichever header is included first won't be complete in the second include file, which will result in your errors.
In the snippet above, there's nothing that I see in encodewindow.h that uses anything in mainwindow.h, so just remove the include of mainwindow.h from encodewindow.h.

Qt no such signal, but it does exist

Qt can't see a signal that exists.
I have a main window that opens up a child window, and the connect statement for that works absolutely fine:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "addstaffform.h"
#include "stafflist.h"
#include "editstaffwindow.h"
#include <QDialog>
#include <QString>
#include <QList>
#include "person.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btn_add_clicked();
void refreshStaffList();
void receiveData(Person& newPerson);
void receiveEditedPerson(Person &editedPerson, int index);
void updateDeleteEnabled();
void updateEditEnabled();
void on_btn_delete_staff_clicked();
void on_btn_staff_edit_clicked();
private:
Ui::MainWindow *ui;
QString s;
QMainWindow *newWin;
QMainWindow *editStaffWin;
StaffList *m_staffList;
QList<Person> m_personList;
Person m_person;
};
#endif // MAINWINDOW_H
The method in mainwindow.cpp that works with no problem:
void MainWindow::on_btn_add_clicked()
{
//Open the add staff window, which is a form
newWin = new AddStaffWindow(this);
//connect the signal from the new form to the slot in this window to receive the person object
connect(newWin, SIGNAL(sendData(Person&)), this, SLOT(receiveData(Person&)));
newWin->show();
}
Signal in addstaffwindow.h (the instantiation of newWin)
signals:
void sendData(Person &p);
Now, in the edit form (editStaffWin), I have the signal, and I've made sure that Q_OBJECT definitely IS there, cleaned, run QMake and build several times, but it doesn't think that the signal exists.
editstaffwindow.h
signals:
void sendPerson(Person &, int index);
So in the mainwindow.cpp for editing, sendPerson(Person &, int) doesn't show up:
void MainWindow::on_btn_staff_edit_clicked()
{
//Get the index of the widget
int index = ui->lst_staff->currentRow();
int size = ui->lst_staff->count();
if (index > -1 && index <= size)
{
//get from staff list
m_person = m_staffList->getStaffAt(index);
editStaffWin = new EditStaffWindow(this, m_person, index);
connect(editStaffWin, SIGNAL(sendPerson(Person &, int)), this, SLOT(receiveEditedPerson(Person&,int)));
editStaffWin->show();
}
}
Any ideas why? I'm not doing anything different for either of them.
EDIT: full EditStaffWindow header:
#ifndef EDITSTAFFWINDOW_H
#define EDITSTAFFWINDOW_H
#include <QMainWindow>
#include "mainwindow.h"
#include "person.h"
#include <QObject>
namespace Ui {
class EditStaffWindow;
}
class EditStaffWindow : public QMainWindow
{
Q_OBJECT
public:
explicit EditStaffWindow(QWidget *parent, Person &p, int index);
~EditStaffWindow();
signals:
void sendPerson(Person &, int index);
private:
Ui::EditStaffWindow *ui;
Person m_person;
int m_index;
public slots:
void showData(Person &p, int index);
private slots:
void on_btn_save_clicked();
void on_btn_cancel_clicked();
};
#endif // EDITSTAFFWINDOW_H
It seems that not using the code autocompletion is probably part of the answer, as I have to manually type the signal
connect(editStaffWin, SIGNAL(sendPerson(Person &, int)), this, SLOT(receiveEditedPerson(Person&,int)));
Running clean, qmake, build all may have been part of it as well. What could have also caused a problem was how I was writing the signal for the connect statement, but looking back, it's the same.
What I did do, though, was remove the int declarations from the sendPerson signal, clean, build, then put them back, and it worked.
Apart from that, I'm not sure what caused it not to work.

QWidget inherit from custom QWidget

In Qt C++, is it possible to create a custom QWidget and then reuse this custom QWidget for all QWidget (that inherit all from the custom QWidget) of the project?
Maybe I have misunderstood the question, but you can just create your custom QWidget, then use it everywhere.
class derivedQWidget : public QWidget
{
Q_OBJECT
derivedQWidget();
virtual ~derivedQWidget();
}
class myWidget : public derivedQWidget
{
...
}
class myWidget2 : public derivedQWidget
{
...
}
If the question is: Can we reimplement QWidget?, no you can't.
i have solved in this mode:
the first class, Widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QPushButton>
#include <QMouseEvent>
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
virtual ~Widget();
QPushButton *getBtn() const;
void setBtn(QPushButton *value);
protected:
void mousePressEvent(QMouseEvent *evt);
void mouseMoveEvent(QMouseEvent *evt);
private:
Ui::Widget *ui;
QPushButton *btn;
QPoint oldPos;
};
and the second class widExt.h, that inherit from Widget:
#ifndef WIDEXT_H
#define WIDEXT_H
#include "widget.h"
namespace Ui {
class widExt;
}
class widExt : public Widget
{
public:
widExt();
private slots:
void on_dial_2_actionTriggered(int action);
private:
Ui::widExt *ui;
};
#endif // WIDEXT_H
with the relative widExt.cpp:
#include "widext.h"
#include "ui_widext.h"
widExt::widExt() : ui(new Ui::widExt)
{
ui->setupUi(this);
}
void widExt::on_dial_2_actionTriggered(int action)
{
}
in this mode, i inherit all from the first class and i can customize other classes independently.