jeudi 8 avril 2021

Program with GUI cannot run which coding by C++&QT6

Platform: Windows 10
IDE: Qt Creator6(latest)
Programming Language: C++11
Compiler: MinGW810_64 g++(latest)
Error Message: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Code segments:
// This is a project called ZorkUL which helps students learn how to do event-driven programming.
// To many files and I'll put what I think are most important segments.
// And if necessary, I'll put all of them.
ZorkUL.h // This is the header file of the main file

#ifndef ZORKUL_H_
#define ZORKUL_H_

#include "Command.h"
#include "Parser.h"
#include "Room.h"
#include "item.h"
#include <iostream>
#include <string>
#include <vector>
class MainWindow;
using namespace std;

class ZorkUL {
private:
    Parser parser;
    Room *currentRoom;
    vector<Room* > rooms;                   // This vector stores every rooms' pointer
    iostream *cinBuffer;
    iostream *coutBuffer;
    void createRooms();
    void printWelcome();
    bool processCommand(Command command);
    void printHelp();
    void goRoom(Command command);
    void createItems();
    void displayItems();
    void tp(Command command);

    friend class MainWindow;

public:
    ZorkUL();
    void play();
    string go(string direction);
};

#endif /*ZORKUL_H_*/

mianWindow.h // This is the header file of the UI file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ZorkUL.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_West_clicked();

    void on_East_clicked();

    void on_Teleport_clicked();

    void on_North_clicked();

    void on_South_clicked();

private:
    Ui::MainWindow *ui;
    ZorkUL *zorkUL;
    string command;
    void commandPreprocess();
};
#endif // MAINWINDOW_H

Part Of ZorkUL.cpp

#include <QApplication>
#include <iostream>
#include <random>
#include <ctime>


using namespace std;
#include "ZorkUL.h"
#include "mainwindow.h"

int main(int argc, char* argv[]) {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

ZorkUL::ZorkUL() {
    createRooms();
}

void ZorkUL::createRooms()  {
    Room *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
    a = new Room("a");
        a->addItem(new Item("x", 1, 11));
        a->addItem(new Item("y", 2, 22));
    b = new Room("b");
        b->addItem(new Item("xx", 3, 33));
        b->addItem(new Item("yy", 4, 44));
    c = new Room("c");
    d = new Room("d");
    e = new Room("e");
    f = new Room("f");
    g = new Room("g");
    h = new Room("h");
    i = new Room("i");
    j = new Room("j");
//             (N, E, S, W)
    a->setExits(f, b, d, c);
    b->setExits(NULL, NULL, NULL, a);
    c->setExits(NULL, a, NULL, NULL);
    d->setExits(a, e, NULL, i);
    e->setExits(NULL, NULL, NULL, d);
    f->setExits(j, g, a, h);
    g->setExits(NULL, NULL, NULL, f);
    h->setExits(NULL, f, NULL, NULL);
    i->setExits(NULL, d, NULL, NULL);
    j->setExits(NULL, NULL, f, NULL);

    currentRoom = a;

    rooms.push_back(a);
    rooms.push_back(b);
    rooms.push_back(c);
    rooms.push_back(d);
    rooms.push_back(e);
    rooms.push_back(f);
    rooms.push_back(g);
    rooms.push_back(h);
    rooms.push_back(i);
    rooms.push_back(j);
}

/**
 *  Main play routine.  Loops until end of play.
 */
void ZorkUL::play() {
    printWelcome();

    // Enter the main command loop.  Here we repeatedly read commands and
    // execute them until the ZorkUL game is over.

    bool finished = false;
    while (!finished) {
        // Create pointer to command and give it a command.
        Command* command = parser.getCommand();
        // Pass dereferenced command and check for end of game.
        finished = processCommand(*command);
        // Free the memory allocated by "parser.getCommand()"
        //   with ("return new Command(...)")
        delete command;
    }
    cout << endl;
    cout << "end" << endl;
}

mainWindow.cpp // With the slots' instance

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <sstream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    zorkUL = new ZorkUL();
    zorkUL->play();
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete zorkUL;
}

void MainWindow::on_West_clicked()
{
    command = "west";
    commandPreprocess();
}

void MainWindow::on_East_clicked()
{
    command = "east";
    commandPreprocess();
}

void MainWindow::on_Teleport_clicked()
{
    command = "tp";
    commandPreprocess();
}

void MainWindow::on_North_clicked()
{
    command = "north";
    commandPreprocess();
}


void MainWindow::on_South_clicked()
{
    command = "south";
    commandPreprocess();
}

void MainWindow::commandPreprocess() 
{// I want to redirection the cin and cout from stdio to the iostream buffer I defined
    string s;
    stringstream ss;
    ss << command;
    zorkUL->cinBuffer = &ss;
    cin.rdbuf(zorkUL->cinBuffer->rdbuf());
    cout.rdbuf(zorkUL->coutBuffer->rdbuf());
    ss.clear();
    ss << zorkUL->coutBuffer;
    ss >> s;
    ui->outBox->append((command + s).c_str());
}

Actually, if you build and run this project, with no errors or warnings the IDE only tell you that The process was ended forcefully.

I searched online and someone tell me copy dll files and paste the files into the same directory as the exe file. I did so and then got the error message mentioned above.

Aucun commentaire:

Enregistrer un commentaire