Qstring error. Saving from Textpool to string

82 views Asked by At

I've been trying to make simple 'typing game' where program is checking if input = showcased word.

I'm trying to use Qt GUI, so I've placed two text widgets, and "play" button. However, there is a problem with string (or QString).

Error: no match for 'operator=' (operand types are 'QString' and 'void')
 wprowadzone = ui->taker->textChanged();
             ^

How can I make this code work? Thanks for any help.

mainwindow.cpp

    #include "mainwindow.h"
#include "ui_mainwindow.h"
#include "game.h"
#include "score.h"
#include <qthread.h>



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

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

void MainWindow::on_actionZako_cz_triggered()
{
    this->close();
}

void MainWindow::on_actionQt_info_triggered()
{
    QApplication::aboutQt();
}

void MainWindow::on_wyjdz_button_clicked()
{
    this->close();
}


void MainWindow::on_graj_button_clicked()
{
    slowo=s.slowa[rand()%s.slowa.size()];
    ui->giver->setText(slowo);
    wprowadzone = ui->taker->textChanged();
    QThread::sleep(5);
    game();
    //ui->SCORE->display(wyniki)
}

void MainWindow::on_SCORE_overflow()
{

}

game.cpp

#include "game.h"
#include "mainwindow.h"
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "slowka.h"
#include <QMainWindow>
#include <QMainWindow.h>
#include "ui_mainwindow.h"
#include <string>
#include <QStringList>




using namespace std;



game::game()
{

    int proby = 0;
    int lives = 3;
    int wynik = 0;

    while (proby > lives)
    {

        if(wprowadzone != slowo)
        {
           wynik -= 1;
           proby += 1;
        }
        else
        {
            wynik += 1;
        }
    }
}

slowka.cpp (words database)

#include "slowka.h"

Slowka::Slowka()
{
    slowa<<"test";
    slowa<<"inny test";
    slowa<<"inniejszy";

}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "slowka.h"



namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

    QString slowo;
    QString wprowadzone;
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private slots:
    void on_actionZako_cz_triggered();

    void on_actionQt_info_triggered();

    void on_wyjdz_button_clicked();

    void on_graj_button_clicked();




    void on_SCORE_overflow();

private:
    Ui::MainWindow *ui;
    Slowka s;

};

#endif // MAINWINDOW_H

game.h

#ifndef GAME_H
#define GAME_H
#include "mainwindow.h"
#include "slowka.h"
#include <QMainWindow>



class game
{
    Q_OBJECT
    QString slowo;
    QString wprowadzone;
public:
    game();
private:
    Slowka s;


};

#endif // GAME_H

slowka.h

#ifndef SLOWKA_H
#define SLOWKA_H
#include <QStringList>

class Slowka
{
public:
    Slowka();
    QStringList slowa;
};

#endif // SLOWKA_H

'graj' is the 'play' button 'proby' is the number of failed tries 'wynik' is the score

1

There are 1 answers

6
Thalia On BEST ANSWER

http://doc.qt.io/qt-4.8/qlineedit.html#text-prop

textChanged is a signal that you can send when the text changes

Use

QString text() const

instead

Another useful method:

modified : bool

to check if the text was modified by the user

Update to answer additional comment questions:

It is best to declare all variables as private.

Add functions to "set" and "get" their value.

The only place you would need the QString mentioned, is in mainwindow.h. In game.h, use the get method to get its value, and the set to change the value.

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void setSlowo(QString s) { slowo = s; }
    QString getSlovo() const { return slovo; }
    // same for wprowadzone

private slots:
    ......

private:
    Ui::MainWindow *ui;
    Slowka s;
    QString slowo;
    QString wprowadzone;

};

Comment I suggest to start with a design, if you write too much code without having a clear picture of what classes you have and how they connect, you will have much trouble later.