Answer the question
In order to leave comments, you need to log in
How to bind a function to a QButton in VS2015 QT5 C++?
Good excerpt. Can't bind a function to a button.
There are 3 files
1. main.cpp
2. other.cpp
3. header.cpp
main.cpp
#include "Header.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget win;
QFileD ofd;
win.setWindowTitle("Hello");
QPushButton *ofdButton = new QPushButton("OPEN", &win);
QPushButton *startButton = new QPushButton("START", &win);
QPushButton *graphButton = new QPushButton("GRAPH", &win);
QPushButton *closeButton = new QPushButton("CLOSE", &win);
QHBoxLayout *layout = new QHBoxLayout(&win);
layout->addWidget(ofdButton);
layout->addWidget(startButton);
layout->addWidget(graphButton);
layout->addWidget(closeButton);
QString filename;
QObject::connect(ofdButton, SIGNAL(s_transmitter()), &win, SLOT(openFile(QString filename)));
QObject::connect(closeButton, SIGNAL(clicked(bool)), &win, SLOT(close()));
win.show();
return app.exec();
}
#pragma once
#include <QtWidgets/QApplication>
#include <QtWidgets>
#include <QtGui>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QApplication>
#include <QFileDialog>
#include <QDebug>
#include <QString>
#include <QDialog>
#include <QHBoxLayout>
#include <iostream>
using namespace std;
using namespace cv;
class QFileD : public QWidget
{
Q_OBJECT
public:
public slots:
void openFile(QString filename);
signals:
void s_transmitter();
};
#include "Header.h"
void QFileD::openFile(QString filename)
{
filename = QFileDialog::getOpenFileName(
this,
"Open Document",
QDir::currentPath(),
"All files (*.*) ;; Document files (*.doc *.rtf);; PNG files (*.png)");
if (!filename.isNull())
{
qDebug() << "selected file path : " << filename.toUtf8();
}
emit s_transmitter();
}
signals:
s_transmitter();
signals:
void s_transmitter();
class QFileD : public Qwidget
class QFileD : public Qobject
Answer the question
In order to leave comments, you need to log in
Everything was decided. Found in an example how to do it. https://wiki.qt.io/How_to_Use_QPushButton/en
QPushButton *ofdButton = new QPushButton("OPEN", &win);
// ...
QObject::connect(ofdButton, SIGNAL(s_transmitter()), &win, SLOT(openFile(QString filename)));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question