From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


Void nie działa jak powinien.



  • Witam!

    Mam funkcje w: mainwindow.h
    @void w_player();@

    i chce ją wywołać w mainwindow.cpp tylko jest problem ponieważ mam w środku ify:
    @void MainWindow::w_player()
    {
    {
    if(ui->button1->text()=="Exit" && ui->button2->text()=="Exit" )
    {
    ui->label_won->setText("2 Exit");
    }

    }
    

    }@

    że jak w 2 przyciskach text = Exit to żeby w label wyświetlił się ... text .
    I jest problem ponieważ funkcja nie wyrzuca błędu a nie robi tego co trzeba.
    Może coś źle robie :/



  • Witam na forum :)

    A sprawdzałeś czy rzeczywiście oba przyciski mają wartość "Exit"?

    Ewentualnie możesz użyć debugera/qDebuga żeby zobaczyć jak wykonywany jest kod.



  • Dobra podam wam cały projekt pisze gre kółko i krzyżyk i tak to wygląda:

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private slots:
    void on_button_left_up_clicked();

    void on_button_left_clicked();
    
    void on_button_left_down_clicked();
    
    void on_button_up_clicked();
    
    void on_button_middle_clicked();
    
    void on_button_down_clicked();
    
    void on_button_right_up_clicked();
    
    void on_button_right_clicked();
    
    void on_button_right_down_clicked();
    
    void w_player();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

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

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

    void MainWindow::on_button_left_up_clicked()
    {
    if(ui->button_left_up->text()=="")
    {
    ui->button_left_up->setText(ui->label_turn_ox->text());
    }
    if(ui->button_left_up->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_left_up->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_left_up->setEnabled(false);
    }

    void MainWindow::on_button_left_clicked()
    {
    if(ui->button_left->text()=="")
    {
    ui->button_left->setText(ui->label_turn_ox->text());
    }
    if(ui->button_left->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_left->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_left->setEnabled(false);
    }

    void MainWindow::on_button_left_down_clicked()
    {
    if(ui->button_left_down->text()=="")
    {
    ui->button_left_down->setText(ui->label_turn_ox->text());
    }
    if(ui->button_left_down->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_left_down->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_left_down->setEnabled(false);
    }

    void MainWindow::on_button_up_clicked()
    {
    if(ui->button_up->text()=="")
    {
    ui->button_up->setText(ui->label_turn_ox->text());
    }
    if(ui->button_up->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_up->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_up->setEnabled(false);
    }

    void MainWindow::on_button_middle_clicked()
    {
    if(ui->button_middle->text()=="")
    {
    ui->button_middle->setText(ui->label_turn_ox->text());
    }
    if(ui->button_middle->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_middle->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_middle->setEnabled(false);
    }

    void MainWindow::on_button_down_clicked()
    {
    if(ui->button_down->text()=="")
    {
    ui->button_down->setText(ui->label_turn_ox->text());
    }
    if(ui->button_down->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_down->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_down->setEnabled(false);
    }

    void MainWindow::on_button_right_up_clicked()
    {
    if(ui->button_right_up->text()=="")
    {
    ui->button_right_up->setText(ui->label_turn_ox->text());
    }
    if(ui->button_right_up->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_right_up->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_right_up->setEnabled(false);
    }

    void MainWindow::on_button_right_clicked()
    {
    if(ui->button_right->text()=="")
    {
    ui->button_right->setText(ui->label_turn_ox->text());
    }
    if(ui->button_right->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_right->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_right->setEnabled(false);
    }

    void MainWindow::on_button_right_down_clicked()
    {
    if(ui->button_right_down->text()=="")
    {
    ui->button_right_down->setText(ui->label_turn_ox->text());
    }
    if(ui->button_right_down->text()=="O")
    {
    ui->label_turn_ox->setText("X");
    }
    if(ui->button_right_down->text()=="X")
    {
    ui->label_turn_ox->setText("O");
    }
    ui->button_right_down->setEnabled(false);
    }

    void MainWindow::w_player()
    {
    {
    if(ui->button_left_down->text()=="X" && ui->button_left->text()=="X" && ui->button_left_up->text()=="X")
    {
    ui->label_won->setText("Won: X");
    }

    }
    

    }
    @
    main.cpp

    @#include "mainwindow.h"
    #include <QApplication>

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

    return a.exec&#40;&#41;;
    

    }
    @



  • Hi,
    try to call w_player() in each on_button_xxx_clicked() functions or use signals-slots connections.



  • Jeżeli to jest cały projekt to ja nie widzę w kodzie ani jednego wywołania metody: @w_player()@
    Jeśli jest to SLOT to pod jaki sygnał go podpiąłeś bo tego też nie widzę.



  • [quote author="turaz" date="1413312214"]Jeżeli to jest cały projekt to ja nie widzę w kodzie ani jednego wywołania funkcji: @w_player()@
    Jeśli jest to SLOT to pod jaki sygnał go podpiąłeś bo tego też nie widzę.
    [/quote]
    Your right. For example can write:
    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    connect(ui->button_left_up, SIGNAL(clicked()),
    this, SLOT(w_player()));
    ...
    }
    @


Log in to reply