Hi, I'm new to Qt and I am using Qt Creator to build my project.
I created a simple project to get familiar with the signal/slot mechanism of Qt.
In created a mainwindow with one pushbutton in the middle.
I created a dialog using a separate UI form with its corrsponding header.
Im trying to get the program to open the dialog window whenever I press
the pushbutton in the main window but I cant seem to get it to work. Where
should I declar the signals, etc to accomplish this? I have tried using
the connect mechanism but even when I modify the mainwindow.cpp
file and try using the pushButton object it says that it is not defined
in this scope. Any help is greatly appreciated!
Here are the 4 files Qt created (UI files are not shown):
mainwindow.cpp is as follows:
//////////////////////////////////////////////////////////////////
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
/*QLabel *headIcon = new QLabel(this); // This is how to display a pic
headIcon->setPixmap(QPixmap("hangmanv10.jpg"));
headIcon->move(100, 200);
headIcon->adjustSize();
headIcon->show();
headIcon->setAttribute(Qt::WA_DeleteOnClose);*/
}
MainWindow::~MainWindow()
{
delete ui;
}
//////////////////////////////////////////////////////
mainwindow.h is as follows:
//////////////////////////////////////////////////////
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "dialog.h"
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//////////////////////////////////////////////////////
dialog.cpp is as follows:
//////////////////////////////////////////////////////
#include "dialog.h"
#include "ui_dialog.h"
Dialog:ialog(QWidget *parent) :
QDialog(parent),
m_ui(new Ui:ialog)
{
m_ui->setupUi(this);
}
Dialog::~Dialog()
{
delete m_ui;
}
void Dialog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
//////////////////////////////////////////////////////
dialog.h is as follows:
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui/QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog {
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
protected:
void changeEvent(QEvent *e);
private:
Ui:ialog *m_ui;
};
#endif // DIALOG_H
//////////////////////////////////////////////////////
and main.cpp is as follows:
//////////////////////////////////////////////////////
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Bookmarks