Witam, piszę program, który ma być prostym serwerem TCP. Kiedy klikamy na przycisk start serwer ma się uruchomić a napis na przycisku ma zmienić się na stop. Napisałem kod taki jak poniżej i podczas debugowania wyskakuje mi taki błąd:
The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x7ffdfaeb157b, code: 0xc0000005: read access violation at: 0xffffffffffffffff, flags=0x0 (first chance).
Kiedy do funkcję:
QObject::connect(serverFlowBtn, &QPushButton::clicked, this, [this]{this->serverFlowBtnClicked();});
Zamienię na:
QObject::connect(serverFlowBtn, &QPushButton::clicked, this, [serverFlowBtn]{serverFlowBtn->setText("Stop");});
Wszystko działa ale ja chciałbym możliwość przekazania do funkcji lambda całego obiektu abym mógł wykonywać na nim metody, w których jest od razu cała logika programu. Co dokładnie oznacza ten błąd? Znalazłem kilka postów na forach i wiem, że chodzi o coś z pointerami, lecz nie wiem jak odnieść to do mojego kodu.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QMessageBox>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
QVBoxLayout* mainLayout;
QTextEdit* logInfo;
QPushButton* clearLogsBtn;
QPushButton* serverFlowBtn;
bool serverRunning;
void serverFlowBtnClicked();
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow()
{
this->resize(800, 600);
serverRunning = false;
mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(10);
mainLayout->setAlignment(Qt::AlignTop);
QTextEdit* logInfo = new QTextEdit(this);
logInfo->setReadOnly(true);
mainLayout->addWidget(logInfo);
QPushButton* serverFlowBtn = new QPushButton("Start", this);
serverFlowBtn->setMinimumHeight(40);
mainLayout->addWidget(serverFlowBtn);
QPushButton* clearLogsBtn = new QPushButton("Clear", this);
clearLogsBtn->setMinimumHeight(40);
mainLayout->addWidget(clearLogsBtn);
QObject::connect(serverFlowBtn, &QPushButton::clicked, this, [this]{this->serverFlowBtnClicked();});
this->show();
}
void MainWindow::serverFlowBtnClicked()
{
this->serverFlowBtn->setText("Stop");
}