0

嘿,我正在给自己写一个游戏,但我不能把应该代表敌人的小部件放在 QGridLayout 中,我已经在 .h 中定义了一个 QStringList 但该函数忽略了 .h 所以网格上的指针和定义必须在函数本身

错误

E:\QT\Try\mainwindow.cpp:56: 
no matching function for call to 
'QGridLayout::addWidget(QPushButton, int&, int&)'
         Grid->addWidget(QPushButton(Enemy_name),b,i);
                                                    ^

E:\QT\Try\mainwindow.cpp:58: Chyba: 'Enemy_list' was not declared in this scope
         Enemy_list[i]=Enemy_name;
         ^

.h

#include <QMainWindow>
#include <QStringList>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    int diff;

    QStringList Enemy_list;


private slots:
    void on_actionNewGame_triggered();

    void on_actionLeft_triggered();

    void on_actionRight_triggered();
private:
    Ui::MainWindow *ui;
    void spawn(int diff);
};

.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"
    #include <QStringList>

    void MainWindow::on_actionNewGame_triggered()
{
    Dialog mDialog;
    mDialog.setModal(true);
    mDialog.exec();

    switch (mDialog.on_buttonBox_accepted()){
    case 2:
        this->setWindowTitle("A not Quality Ripoff -- Medium");
        spawn(2);
        break;
    case 3:
        spawn(3);
        this->setWindowTitle("A not Quality Ripoff -- Hard");
        break;
    default:
        spawn(1);
        this->setWindowTitle("A not Quality Ripoff -- Easy");
        break;
    }
}
void spawn(int diff){
    Ui::MainWindow *kappa;
    const int row = diff*4;
    int col;
    int x=0;
    switch (diff){
    case 3:
        x=x+10;
    case 2:
        x=x+10;
    default:
        x=x+20;
        col = x/row;
        if (x %row >=1) col++;

        for(int i=0;i<col;i++){
            for(int b=0; b<row;b++){

            QString Enemy_name = "Enemy_n"+QString::number(i*b);

            QGridLayout *Grid = kappa->Enemy;
            Grid->addWidget(QPushButton(Enemy_name),b,i);

            Enemy_list[i]=Enemy_name;
            }
        }
        break;
    }
}

作为初学者,我会很高兴得到任何帮助

4

1 回答 1

0

第一个错误:将行更改Grid->addWidget(QPushButton(Enemy_name),b,i);Grid->addWidget(new QPushButton(Enemy_name),b,i);

第二个错误:将行更改void spawn(int diff){void MainWindow::spawn(int diff){

于 2017-04-13T11:51:50.987 回答