嘿我自己写了一个游戏,但是我不能把那些应该代表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;
}
}
作为初学者,我会很高兴得到任何帮助
答案 0 :(得分: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){