これはおそらく私がしなければならない簡単なことです.1つ以上のクラスからuiにアクセスしようとしています.mainwindowクラスでは機能しますが、Socketクラスでは機能しません. ソケット クラスから ui のディスプレイに書き込みたいのですが、ui と入力すると、. 自動的に ui-> が作成され、多くのオプションが表示されます。ここにmainwindow.hがあります
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include "studentlist.h"
#include "student.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;
private slots:
void on_buttonGraduate_clicked();
void on_buttonAverage_clicked();
void on_buttonDisplay_clicked();
void on_buttonAddModule_clicked();
void acceptNewStudent();
void processFinished(int);
protected:
private:
QProcess* process;
StudentList* studentList;
void displayDetail(QString msg, AbstractStudent* asp);
int doCheck();
void closeEvent(QCloseEvent *);
};
#endif // MAINWINDOW_H
およびsocket.h
#ifndef SOCKET_H
#define SOCKET_H
#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QMainWindow>
#include "mainwindow.h"
#include "ui_mainwindow.h"
class Socket : public QObject
{
Q_OBJECT
public:
explicit Socket(QObject *parent = 0);
void run();
signals:
public slots:
void connected();
void disconnected();
void bytesWritten(qint64 bytes);
void readyRead();
private:
QTcpSocket *socket;
};
#endif // SOCKET_H
.cpp ファイルの追加
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QMessageBox>
#include <QFile>
#include "studentserializer.h"
#include <QTextStream>
#include <QCloseEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
process = 0;
studentList = StudentList::getInstance();
}
MainWindow::~MainWindow()
{
delete process;
delete ui;
}
void MainWindow::on_buttonAddModule_clicked()
{
process = new QProcess(this);
process->start("C:/Unisa/COS3711/Solutions to assignment 2/S2A2Q4ProcessStudent-build-desktop/debug/S2A2Q1GetStudent.exe");
connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(acceptNewStudent()));
connect(process, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
}
void MainWindow::acceptNewStudent()
{
QByteArray bytes = process->readAllStandardOutput();
QStringList items = QString(bytes).split("#");
QString number = items.at(0);
QString module = items.at(1);
int mark = items.at(2).toInt();
//check if student already exists
int index = studentList->exists(number);
if (index == -1) // student does not yet exist
{
Student* student = new Student;
student->setNumber(number);
student->addModule(module, mark);
studentList->addStudent(student);
displayDetail("New student added", student);
}
else // student does exist
{
AbstractStudent* a = studentList->getStudent(index);
a->addModule(module, mark);
displayDetail("Updated student", a);
a = 0;
}
}
void MainWindow::on_buttonDisplay_clicked()
{
int index = doCheck();
if (index >=0)
{
AbstractStudent* asp = studentList->getStudent(index);
displayDetail("Displaying student", asp);
asp = 0;
}
}
void MainWindow::displayDetail(QString msg, AbstractStudent* asp)
{
ui->display->clear();
ui->display->append(msg);
ui->display->append("Number: " + asp->getNumber());
QMap<QString, int> mods = asp->getModules();
QMapIterator<QString, int> i(mods);
while (i.hasNext())
{
i.next();
ui->display->append("Module: " + i.key() + " Mark: " + QString::number(i.value()));
}
}
void MainWindow::on_buttonAverage_clicked()
{
int index = doCheck();
if (index>=0)
{
AbstractStudent* asp = studentList->getStudent(index);
displayDetail("Displaying student", asp);
ui->display->append("Module average: " + QString::number(asp->average()));
asp = 0;
}
}
void MainWindow::on_buttonGraduate_clicked()
{
int index = doCheck();
if (index>=0)
{
AbstractStudent* asp = studentList->getStudent(index);
QString msg = asp->graduate()?"This student graduates":"This student does not graduate";
displayDetail(msg, asp);
asp = 0;
}
}
int MainWindow::doCheck()
{
QMessageBox warning;
warning.setIcon(QMessageBox::Critical);
int index=-1;
if (ui->studentNumber->text().size() == 0)
{
warning.setText("Provide a number");
warning.exec();
index = -1;
}
else
{
index = studentList->exists(ui->studentNumber->text());
if (index == -1)
{
warning.setText("Number does not exist");
warning.exec();
}
}
ui->studentNumber->clear();
ui->studentNumber->setFocus();
ui->display->clear();
return index;
}
void MainWindow::closeEvent(QCloseEvent* event)
{
QFile studentFile("studentlist.xml");
studentFile.open(QIODevice::WriteOnly);
QTextStream toFile(&studentFile);
StudentSerializer s;
QList<AbstractStudent*>* list = studentList->returnList();
for (int i=0; i<list->size(); i++)
s.addStudent(list->at(i));
QDomDocument tempDoc = s.getDoc();
toFile << tempDoc.toString();
studentFile.close();
event->accept();
}
void MainWindow::processFinished(int)
{
process->close();
process->deleteLater();
process=0;
}
そしてsocket.cpp
#include "socket.h"
Socket::Socket(QObject *parent) :
QObject(parent)
{
}
void Socket::run(){
socket = new QTcpSocket(this);
connect(socket,SIGNAL(connected()),this,SLOT(connected()));
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWritten(qint64)));
qDebug() << "Connecting...";
socket->connectToHost("twitter.com",80);
if (!socket->waitForConnected(3000))
{
qDebug() << "error:" << socket->errorString();
}
}
void Socket::connected(){
qDebug() << "Connected";
}
void Socket::disconnected(){
qDebug() << "Disconected";
}
void Socket::bytesWritten(qint64 bytes){
qDebug() << "we wrote: " << bytes;
}
void Socket::readyRead(){
qDebug() << "Reading...";
socket->readAll();
}
これらのqDebugは、displayと呼ばれるQTextEditに書きたいと思います