我有一个用c ++编写的控制台游戏,因为我正在尝试学习Qt,所以我决定将GUI添加到这个程序作为练习。 因此,主窗口名为" gui"继承了Qwidget。它有布局QHBoxLayout * main_h_lo。其中有2个添加的布局:1。QStackedLayout * leftpart,2。QGridLayout * deck。第一个是某种菜单部分。它有4种不同的小部件及其布局。例如选择游戏模式或打印游戏分数。第二个布局 - 甲板 - 是游戏桌,类似于棋盘。 我认为我的构造函数代码包含问题:
gui::gui(QWidget *parent) :
QWidget(parent), pgame(nullptr)
{
QHBoxLayout* main_h_lo = new QHBoxLayout;
main_h_lo->setMargin(0);
main_h_lo->setSpacing(0);
setLayout(main_h_lo);
//leftpart-widgets initialization:
bot_or_playerW = new QWidget;
QVBoxLayout* bot_or_playerL = new QVBoxLayout;
bot_or_playerL->addWidget(new QLabel("Choose game mode"));
QPushButton* qpb1 = new QPushButton("vs Human");
QPushButton* qpb2 = new QPushButton("vs Bot");
QObject::connect(qpb1, SIGNAL(clicked()), SLOT(pvp()));
QObject::connect(qpb2, SIGNAL(clicked()), SLOT(pvb()));
bot_or_playerL->addWidget(qpb1);
bot_or_playerL->addWidget(qpb2);
bot_or_playerW->setLayout(bot_or_playerL);
choosing_colourW = new QWidget;
QVBoxLayout* choosing_colourL = new QVBoxLayout;
choosing_colourL->addWidget(new QLabel("Choose your colour"));
QPushButton* qpb3 = new QPushButton("white(2nd turn)");
QPushButton* qpb4 = new QPushButton("black(1st turn)");
QObject::connect(qpb3, SIGNAL(clicked()), SLOT(chwh()));
QObject::connect(qpb4, SIGNAL(clicked()), SLOT(chbl()));
choosing_colourL->addWidget(qpb3);
choosing_colourL->addWidget(qpb4);
choosing_colourW->setLayout(bot_or_playerL);
score_lturnW = new QWidget;
QVBoxLayout* score_lturnL = new QVBoxLayout;
lturn = new QLabel;
pturn = new QLabel;
score = new QLabel;
score_lturnL->addWidget(lturn);
score_lturnL->addWidget(pturn);
score_lturnL->addWidget(score);
score_lturnW->setLayout(score_lturnL);
after_gameW = new QWidget;
QVBoxLayout* after_gameL = new QVBoxLayout;
winner = new QLabel;
offer_to_play_again = new QLabel("Wanna play again?");
QPushButton* qpb5 = new QPushButton("yes");
QObject::connect(qpb5, SIGNAL(clicked()), SLOT(restart()));
QPushButton* qpb6 = new QPushButton("no");
QObject::connect(qpb6, SIGNAL(clicked()), qApp, SLOT(quit()));
after_gameW->setLayout(after_gameL);
leftpart = new QStackedLayout;
leftpart->addWidget(bot_or_playerW);
leftpart->addWidget(choosing_colourW);
leftpart->addWidget(score_lturnW);
leftpart->addWidget(after_gameW);
//"rightpart" init:
deck = new QGridLayout;
deck->setMargin(0);
deck->setSpacing(0);
e_pic = QPixmap("empty.png");
b_pic = QPixmap("black.png");
w_pic = QPixmap("white.png");
pic_sz = e_pic.size();
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
{
QPushButton* tqpb = new QPushButton;
tqpb->setIcon(e_pic);
tqpb->setIconSize(pic_sz);
std::stringstream ss;
std::string s;
ss << i << j;
ss >> s;
tqpb->setObjectName(s.c_str());
deck->addWidget(tqpb, i, j);
connect(tqpb, SIGNAL(clicked()), SLOT(turn_try()));
}
main_h_lo->addLayout(leftpart);
main_h_lo->addLayout(deck);
leftpart->setCurrentWidget(bot_or_playerW);
}
我没有收到任何错误或警告。甲板部分是可怕和丑陋的,但它是如预期的那样:D。 &#34;菜单&#34;部分没有出现 - 这就是问题所在。屏幕:http://i.imgur.com/Sh9PU9N.jpg。
答案 0 :(得分:0)
关于你代码的一些评论 -
可以在构造中为父级提供布局,它将自动成为默认布局。这可以为您节省一个命令,但会使其更加隐含。这一切都取决于你喜欢什么 - 隐性或明确。
松开QObject :: connect。一个简单的'连接'就行了。
您确定“黑色(第一圈)”是否正确?在传统的国际象棋游戏中,白色通常是第一位的。
您可以避免使用std :: stringstream并使用QString :: number。
'hyde'已经提到了这个问题。