So, here are my project files (I've split the class "actionObj" from the mainwindow file).
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include "actions.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
NewButton->setText("Button");
actionsObj *ao;
ao = new actionsObj;
connect(NewButton, SIGNAL(clicked()), this,SLOT(ButtonClicked()));
NewButton->setGeometry(50, 40, 300, 200);
NewButton->show();
// ui->setupUi(this); if I uncomment this, the button isn't clickable!
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include "actions.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QPushButton *NewButton= new QPushButton(this);
NewButton->setText("Button");
actionsObj *ao;
ao = new actionsObj;
connect(NewButton, SIGNAL(clicked()), this,SLOT(ButtonClicked()));
NewButton->setGeometry(50, 40, 300, 200);
NewButton->show();
// ui->setupUi(this); if I uncomment this, the button isn't clickable!
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
actions.cpp:
#include "actions.h"
#include <QMessageBox>
void actionsObj ::ButtonClicked()
{
msg.setText("Hello. You've clicked the button");
msg.exec();
}
#include "actions.h"
#include <QMessageBox>
void actionsObj ::ButtonClicked()
{
QMessageBox msg;
msg.setText("Hello. You've clicked the button");
msg.exec();
}
To copy to clipboard, switch view to plain text mode
actions.h:
#ifndef ACTIONS_H
#define ACTIONS_H
#include <qobject.h>
{
private:
Q_OBJECT
public slots:
void ButtonClicked();
};
#endif // ACTIONS_H
#ifndef ACTIONS_H
#define ACTIONS_H
#include <qobject.h>
class actionsObj : public QObject
{
private:
Q_OBJECT
public slots:
void ButtonClicked();
};
#endif // ACTIONS_H
To copy to clipboard, switch view to plain text mode
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QAction>
#include <QString>
int main(int argc, char *argv[])
{
MainWindow w;
w.setGeometry(100, 100, 500, 500);
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QAction>
#include <QString>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString title="MainWindow";
MainWindow w;
w.setGeometry(100, 100, 500, 500);
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
But now I have the following error:
collect2: Id returned 1 exit status
and the IDE doesn't bring me to the line affected...what could be the problem? it's possible that a simple program like this, even if I'm a starter, is so complicated and full of problems??
Thanks a lot
Bookmarks