I create a main window, which opens a new window on a button click. That works fine, but the new window's button doesn't respond to clicks. I have included Q_OBJECT in the class declaration. The code is as follows:
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "AnotherWindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "AnotherWindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QObject>
#include <QWidget>
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void TestFunction();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QObject>
#include <QWidget>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void TestFunction();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include <QtGui>
#include "mainwindow.h"
#include "AnotherWindow.h"
MainWindow
::MainWindow(QWidget *parent
){
layout->addWidget(pb);
this->setLayout(layout);
connect(pb, SIGNAL( clicked() ), this, SLOT( TestFunction() ));
}
void MainWindow::TestFunction()
{
qDebug("This is a test");
AnotherWindow aw;
}
MainWindow::~MainWindow()
{
}
#include <QtGui>
#include "mainwindow.h"
#include "AnotherWindow.h"
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
QPushButton* pb = new QPushButton("Test");
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(pb);
this->setLayout(layout);
connect(pb, SIGNAL( clicked() ), this, SLOT( TestFunction() ));
}
void MainWindow::TestFunction()
{
qDebug("This is a test");
AnotherWindow aw;
}
MainWindow::~MainWindow()
{
}
To copy to clipboard, switch view to plain text mode
AnotherWindow.h:
#ifndef ANOTHERWINDOW_H
#define ANOTHERWINDOW_H
#include <QObject>
#include <QWidget>
class AnotherWindow
: public QWidget{
Q_OBJECT
public:
AnotherWindow
(QWidget* parent
= 0);
private slots:
void AnotherTest();
}
;
#endif // ANOTHERWINDOW_H
#ifndef ANOTHERWINDOW_H
#define ANOTHERWINDOW_H
#include <QObject>
#include <QWidget>
class AnotherWindow : public QWidget
{
Q_OBJECT
public:
AnotherWindow(QWidget* parent = 0);
private slots:
void AnotherTest();
}
;
#endif // ANOTHERWINDOW_H
To copy to clipboard, switch view to plain text mode
AnotherWindow.cpp:
#include <QtGui>
#include <QDebug>
#include "AnotherWindow.h"
AnotherWindow
::AnotherWindow(QWidget* parent
){
anotherLayout->addWidget(pushy);
connect(pushy, SIGNAL(clicked()), this, SLOT(AnotherTest()));
window->setLayout(anotherLayout);
window->show();
}
void AnotherWindow::AnotherTest()
{
qDebug("Why isn't this code hit?");
}
#include <QtGui>
#include <QDebug>
#include "AnotherWindow.h"
AnotherWindow::AnotherWindow(QWidget* parent)
{
QWidget* window = new QWidget;
QVBoxLayout* anotherLayout = new QVBoxLayout;
QPushButton* pushy = new QPushButton("OMGWTFBBQ");
anotherLayout->addWidget(pushy);
connect(pushy, SIGNAL(clicked()), this, SLOT(AnotherTest()));
window->setLayout(anotherLayout);
window->show();
}
void AnotherWindow::AnotherTest()
{
qDebug("Why isn't this code hit?");
}
To copy to clipboard, switch view to plain text mode
Bookmarks