I get this error when run program in qt 5.8 in windows 10 64 bit. how to solve this issue.
My .pro file
00:43:14: Running steps for project untitled1...
00:43:16: Starting: "G:\IDE\Qt5.8.0\5.8\mingw53_32\bin\qmake.exe" "G:\Qt Projects\untitled1\untitled1.pro" -spec win32-g++ "CONFIG+=qml_debug"
MAKEFILE_GENERATOR variable not set as a result of parsing : untitled1.pro. Possibly qmake was not able to find files included using "include(..)" - enable qmake debugging to investigate more.
00:43:32: The process "G:\IDE\Qt5.8.0\5.8\mingw53_32\bin\qmake.exe" exited with code 3.
Error while building/deploying project untitled1 (kit: Desktop Qt 5.8.0 MinGW 32bit)
When executing step "qmake"
00:43:32: Elapsed time: 00:18.
Header File
#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:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp file
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui file
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
please help me to fix this issue. Thanks
Related
With Qt Designer, when I promote Widget, I've got some problem with ui_*.h files and the include of the headers of the promoted class. The problem appears under Linux where the headers of the promoted class is not found. Under Windows with MSVC 2017, no problem ...
Under windows, I put the relative path from the *.ui file folder.
Under Linux, it seems that I have to put the relative from the *.pro file folder ... not great if I have to reuse the widget in another project but it works
Anyone can explain ?
Thanks !
EDIT :
I have added example code bellow.
Do you need the generated ui_ file ?
You can see on this last file that the path to the mycombobox.h is relative from the root folder and not from the *.ui file folder.
This configuration seem to work under Windows too, but I would like to put the relative path from the *.ui file.
Structure of my folder :
in the root :
main.cpp
mainWindows.h
mainwindows.cpp
/Folder1 :
MyComboBox.h
/Folder1/Folder2 :
Form.h
Form.cpp
the main.cpp file :
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The mainwindows.h :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QHBoxLayout>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
The mainwindows.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "folder1/folder2/form.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
centralWidget()->setLayout(new QHBoxLayout);
centralWidget()->layout()->addWidget(new Form);
}
MainWindow::~MainWindow()
{
delete ui;
}
The /folder1/MyComboBox.h
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QComboBox>
class MyComboBox:public QComboBox
{
Q_OBJECT
public:
MyComboBox(QWidget* parent=nullptr):QComboBox(parent)
{
addItem("My ComboBox");
}
virtual ~MyComboBox() {}
};
The /folder1/folder2/Form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
private:
Ui::Form *ui;
};
The /folder1/folder2/Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
Last but not least, the Form.ui file :
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="MyComboBox" name="comboBox"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MyComboBox</class>
<extends>QComboBox</extends>
<header>folder1/mycombobox.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
A possible solution is to use a .pri where the files that handle the form will be placed:
├── 52435692.pro
├── Folder1
│ ├── Folder2
│ │ ├── form.cpp
│ │ ├── form.h
│ │ ├── form.pri
│ │ └── form.ui
│ └── mycombobox.h
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
└── mainwindow.ui
form.pri
INCLUDEPATH += $$PWD
SOURCES += $$PWD/form.cpp
HEADERS += $$PWD/form.h \
$$PWD/../mycombobox.h
FORMS += $$PWD/form.ui
*.pro
...
include(Folder1/Folder2/form.pri)
form.ui
...
<customwidget>
<class>MyComboBox</class>
<extends>QComboBox</extends>
<header>../mycombobox.h
</header>
</customwidget>
...
Also, when using INCLUDEPATH += $$PWD in the .pri it is no longer necessary to include the complete path when importing the form:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include "form.h" // <--- before "folder1/folder2/form.h"
...
The complete example can be found in the following link
I am trying to build a form with several LineEdits and I need an OpenFileDialog to select a Path and write it into one of the LineEdits.
The problem is, this form is generated "dynamically".
All of this needs to happen inside a function call, in which the Dialog and all LineEdits are created, so i can't just add a Button, link the clicked Signal to a new Slot, open the File Dialog in the Slot and write the result into the LineEdit.
Here is my Code from inside the function:
QDialog dialog(this);
QFormLayout form(&dialog);
QList<QLineEdit *> fields;
QLineEdit *lineEdit = new QLineEdit(&dialog);
QString label = QString("Name");
form.addRow(label, lineEdit);
fields << lineEdit;
lineEdit = new QLineEdit(&dialog);
label = QString("Time");
form.addRow(label, lineEdit);
fields << lineEdit;
lineEdit = new QLineEdit(&dialog);
label = QString("Number #1");
form.addRow(label, lineEdit);
fields << lineEdit;
lineEdit = new QLineEdit(&dialog);
label = QString("Number #2");
form.addRow(label, lineEdit);
fields << lineEdit;
lineEdit = new QLineEdit(&dialog);
label = QString("Number #3");
form.addRow(label, lineEdit);
fields << lineEdit;
//--------------Here should be the path selection
lineEdit = new QLineEdit(&dialog);
label = QString("Path");
form.addRow(label, lineEdit);
fields << lineEdit;
//-----------------------------------------------
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,Qt::Horizontal, &dialog);
form.addRow(&buttonBox);
QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
if (dialog.exec() == QDialog::Accepted) {
//...Do stuff with the Text from the Line Edits
Is there a way to do this without making things global, so a Slot could access it?
Because I really like the idea of this form completely staying inside the function call.
If I understood the issue correctly, I think you need to redesign the application.
You need some variables in your class and update the dialog when the user changes the QLineEdit path. But to do this, you need the dialog as a member variable.
The next code is just an example, but it's a good way of showing you a way of solving this. Of course, there are many different options to do it.
Note #1: I don't know when you launch the dialog, so I've created a very basic ui with a button to display it.
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>160</x>
<y>120</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</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 <QMainWindow>
#include <QLineEdit>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void updateValues(const QString & text);
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QLineEdit* path;
QLineEdit* name;
QLineEdit* time;
QLineEdit* number;
QDialog* dialog;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QFormLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dialog = new QDialog(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QFormLayout form(dialog);
name = new QLineEdit(dialog);
QString label = QString("Name");
form.addRow(label, name);
time = new QLineEdit(dialog);
label = QString("Time");
form.addRow(label, time);
number = new QLineEdit(dialog);
label = QString("Number #1");
form.addRow(label, number);
path = new QLineEdit(dialog);
label = QString("Path");
form.addRow(label, path);
connect(path, &QLineEdit::textChanged,
this, &MainWindow::updateValues );
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,Qt::Horizontal, dialog);
form.addRow(&buttonBox);
QObject::connect(&buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
QObject::connect(&buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
dialog->exec();
}
void MainWindow::updateValues(const QString & text)
{
name->setText(text);
time->setText(text);
number->setText(text);
}
I created a simple project to show the problem I have in a bigger application.
So, I create a mainwindow with 2 buttons. I create a class which inherit from QWidget with 2 buttons and a QGL view.
The problem is that, apparently and for some reason I don't find, the creation of this QGL view blocks some events of the views, particularly mouse events. In the code below, the mouse hover events on the buttons and the main widget of class 1 are not detected (the cursor has to change from Arrow to hand and wait respectively) whereas the mouse cursor changes well on the mainwindow buttons (but in my real application, it's more serious than a simple hover event).
I precise that this problem appears only on Mac 10.6 and superior, and not on Windows. I use Qt 5.1.
Here is the code :
mainwindow.h :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class Class1;
class PanoramaGLView;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Class1 *c1;
PanoramaGLView *gl;
};
#endif // MAINWINDOW_H
mainwindoww.cpp :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
using namespace std;
#include "class1.h"
#include "panoramaglview.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
c1=new Class1(0);
ui->centralLayout->addWidget(c1);
//if created here, no problem with mouse cursor on mainwindow buttons but not what I want
//gl=new PanoramaGLView(0);
//ui->centralLayout->addWidget(gl);
}
MainWindow::~MainWindow()
{
delete ui;
}
myglview.h :
#ifndef MYGLVIEW_H
#define MYGLVIEW_H
#include <QGraphicsView>
#include <QtOpenGL/QGLWidget>
class MyGLView : public QGraphicsView
{
Q_OBJECT
public:
MyGLView(QWidget* pParent = 0);
virtual ~MyGLView();
protected:
QGLWidget* m_pGLWidget;
};
#endif // MYGLVIEW_H
myglview.cpp :
#include "myglview.h"
MyGLView::MyGLView(QWidget* pParent)
: QGraphicsView(pParent)
{
m_pGLWidget = new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::AlphaChannel));
m_pGLWidget->makeCurrent();
setViewport(m_pGLWidget);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}
MyGLView::~MyGLView()
{
delete m_pGLWidget;
}
class1.h :
#ifndef CLASS1_H
#define CLASS1_H
#include <QWidget>
class MyGLView;
namespace Ui {
class Class1;
}
class Class1 : public QWidget
{
Q_OBJECT
public:
explicit Class1(QWidget *parent = 0);
~Class1();
private:
Ui::Class1 *ui;
public slots:
void click1();
};
#endif // CLASS1_H
class1.cpp :
#include "class1.h"
#include "ui_class1.h"
#include "myglview.h"
#include <iostream>
using namespace std;
Class1::Class1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Class1)
{
ui->setupUi(this);
MyGLView *glv=new MyGLView(0);
ui->verticalLayout->addWidget(glv);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(click1()));
}
Class1::~Class1()
{
delete ui;
}
void Class1::click1(){
cout<<"Class1::click1()"<<endl;
}
mainwindow.ui :
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="centralLayout">
<item>
<widget class="QWidget" name="widget" native="true">
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 54, 76);</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
class1.ui :
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Class1</class>
<widget class="QWidget" name="Class1">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="cursor">
<cursorShape>WaitCursor</cursorShape>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
If I don't create the GL view, there is no problem of course. And if I create the myGLView instance directly in MainWindow, without class1, it works too. But I need to create différents views and not all in mainwindow (which is usually how ui are created).
In my opinion, it's a problem with the way I create the view, its parent or something like that, as event are passed from parent to child, so I think the problem has a link with that. If it works well, the mouse cursor has to change when the mouse passes over the buttons 1 and 2 (hand cursor) and just hover class1 main widget (waiting cursor).
Any ideas?
I can confirm that this is a regression. Tested on OS X 10.8.4. It works under Qt 4.8.5, doesn't work under Qt 5.1.0. You should report it as a bug. Below is a sane single-file test case that you will know to produce the next time you post to stackoverflow :)
//main.cpp
#include <QApplication>
#include <QPushButton>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QGraphicsView>
#include <QGLWidget>
class TestView : public QGraphicsView
{
public:
explicit TestView(QWidget* parent = 0) : QGraphicsView(parent) {
setViewport(new QGLWidget());
setScene(new QGraphicsScene(this));
scene()->addEllipse(-50, -50, 100, 100, QPen(Qt::red), QBrush(Qt::lightGray));
}
};
class Pane : public QWidget
{
public:
explicit Pane(bool hasView, const QCursor & cur, QWidget *parent = 0) :
QWidget(parent)
{
QVBoxLayout * l = new QVBoxLayout(this);
QPushButton * btn = new QPushButton("[Pane]");
btn->setCursor(cur);
l->addWidget(btn);
if (hasView) l->addWidget(new TestView()); else l->addStretch();
}
};
class MainWindow : public QWidget
{
Q_OBJECT
QStackedWidget *sw;
public:
explicit MainWindow(QWidget *parent = 0) : QWidget(parent) {
QVBoxLayout *l = new QVBoxLayout(this);
QPushButton *btn = new QPushButton("[Main Window] Flip Pages");
btn->setCursor(Qt::PointingHandCursor);
connect(btn, SIGNAL(clicked()), SLOT(nextPage()));
sw = new QStackedWidget();
l->addWidget(btn);
l->addWidget(sw);
sw->addWidget(new Pane(true, Qt::OpenHandCursor));
sw->addWidget(new Pane(false, Qt::ClosedHandCursor));
}
Q_SLOT void nextPage() { sw->setCurrentIndex((sw->currentIndex() + 1) % sw->count()); }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
I'm implementing a QtableView with the data from a QSqlQueryModeL.
What happens is, if I select a item in the view, the item after a second disappears and the items in the other rows disappear as well.
my code:
ui->medicstableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->medicstableView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->medicstableView->setStyleSheet(QString::fromUtf8("background-color: rgb(227, 226, 226);"));
//ui->medicstableView->setVisible(true);
//ui->medicstableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
doctors_table = new QSqlQueryModel(this);
QSqlDatabase mydb;
QString Squery;
QString conn = mydb.connectionName();
if (conn.isEmpty()) {
mydb = QSqlDatabase::addDatabase("QMYSQL");
}
else
{
mydb.close();
QSqlDatabase::removeDatabase(conn);
mydb = QSqlDatabase::addDatabase("QMYSQL");
}
mydb.setHostName(myhost);
mydb.setUserName(myuser);
mydb.setPassword(mypass);
mydb.setDatabaseName(mybase);
try
{
if(mydb.open()==true)
{
Squery=QString("SELECT id_medico,medico FROM Registos_Medicos");
doctors_table->setQuery(Squery);
doctors_table->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
doctors_table->setHeaderData(1, Qt::Horizontal, QObject::tr("Nome do médico"));
ui->medicstableView->setModel(doctors_table);
mydb.close();
//ui->statusbar->addAction();
ui->medicstableView->resizeColumnsToContents ();
ui->medicstableView->resizeRowsToContents ();
ui->medicstableView->setColumnWidth(1,255);
ui->medicstableView->show();
The QTableView is created in the Qt Designer with the name "medicstableView"
Thanks in advance.
ok let's try with this. It works to me:
//pro file:
QT += core gui sql
TARGET = test1
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp \
database.cpp
HEADERS += widget.h \
database.h
FORMS += widget.ui
//database.h:
#ifndef DATABASE_H
#define DATABASE_H
#include <QSqlDatabase>
class Database
{
public:
Database();
~Database();
QSqlDatabase db;
bool connection();
void createTables();
};
#endif // DATABASE_H
//database.cpp:
#include "database.h"
#include <QtGui>
#include <QSqlQuery>
Database::Database()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
}
Database::~Database()
{
db.close();
}
bool Database::connection()
{
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Ok);
return false;
}
return true;
}
void Database::createTables()
{
QSqlQuery q;
q.exec("CREATE TABLE test(id integer primary key,name varchar(20))");
q.exec("INSERT INTO test(name) VALUES('foo')");
q.exec("INSERT INTO test(name) VALUES('fie')");
q.exec("INSERT INTO test(name) VALUES('bar')");
}
//widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QSqlQueryModel;
class Database;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
QSqlQueryModel *model;
Database *m_db;
Ui::Widget *ui;
};
#endif // WIDGET_H
//widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include "database.h"
#include <QSqlQueryModel>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_db = new Database();
m_db->connection();
m_db->createTables();
model = new QSqlQueryModel();
model->setQuery("SELECT * from test");
ui->tableView->setModel(model);
}
Widget::~Widget()
{
delete ui;
}
//widget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>274</width>
<height>210</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
i guess your mistake is close db connection. Db connection must be closed on application closure
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:
Ui::MainWindow *ui;
};
#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;
}
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QObject::connect(pushButton, SIGNAL(clicked()),
&a, SLOT(quit()));
return a.exec();
}
All the codes are given above. In a general Qt GUI program, I put a pushButton on the UI Form and tried to use it in main.cpp. But below error was given:
main.cpp:10: Error:'pushButton' was not declared in this scope
Could you please give me a solution? How can I call it in main.cpp?
Thanks!
Complement1:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->pushButton, SIGNAL(clicked()),
QCoreApplication::instance(), SLOT(close()));
}
MainWindow::~MainWindow()
{
delete ui;
}
If I do it this way, then the program can run but cannot close the whole application. I guess that is because the QCoreApplication::instance() returns null due to in the constructor phase the QApplication doesn't exist, right?
Complement2:
mainwindow.cpp
void MainWindow::on_pushButton_clicked()
{
close();
}
One solution is to add new slot of the pushButton in mainwindow.cpp, like above.
But still I hope to know how to do it my way (main part of this post)?
Complement3:
Alberto's code works fine by using QWidget as below way.
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
Your first approach can't work because, as compiler says, you didn't defined a QPushButton in main.cpp scope. So compiler can't find it.
Right approach is your last one. You develop a .ui file in which you add a QPushButton and then in c++ code that handles your .ui file(in your example a QMainWindow subclass) you connect it with a slot. But your connect call is wrong. This is my code:
//widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
Widget::~Widget()
{
delete ui;
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
}
//widget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>93</width>
<height>41</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
//main.cpp:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
with: connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
which we can simplify in this way: connect(1,2,3,4);
when 2 is emitted because user does something on 1, do 4 on 3.
translated: when user click on pushButton(defined into ui file), do close on this(QMainWindow subclass). And if you take a look at your main.cpp code, MainWindow is the main widget you show to the user. So you're closing the whole app.