有人可以帮我解决我的代码问题
我在下面的代码中使用了2个按钮(在Qt GUi程序中)(第1版,第2版)
初始图片
1)当我按下第一个按钮时,Gbox中的输出..按下第一个按钮时
点击第一个按钮
2)当我按下第二按钮时,按下第二个按钮后,预期输出应该像Gbox中的预期输出
第二按钮单击时需要
但是按下Gbox中的第二个按钮后没有任何变化..按下第二个按钮后没有变化
选择Gbox_2nd按钮中的No_changes
#include "test.h"
#include "ui_test.h"
#include <QPushButton>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QtWidgets>
#include <QLineEdit>
#include <QTextEdit>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QGroupBox>
test::test(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::test)
{
ui->setupUi(this);
QVBoxLayout *mainLayout = new QVBoxLayout;
ui->label_1->hide();
ui->label_2->hide();
ui->label_3->hide();
ui->label_4->hide();
ui->label_5->hide();
ui->label_6->hide();
ui->label_7->hide();
ui->lineEdit_1->hide();
ui->lineEdit_2->hide();
ui->lineEdit_3->hide();
ui->lineEdit_4->hide();
ui->lineEdit_5->hide();
ui->lineEdit_6->hide();
ui->lineEdit_7->hide();
ui->Optional->hide();
Gbox->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
Gbox->setTitle(tr("Gbox_11"));
mainLayout->addWidget(Gbox);
setLayout(mainLayout);
}
test::~test()
{
delete ui;
}
void test::on_1st_clicked()
{
ui->label_1->show();
ui->lineEdit_1->show();
ui->label_1->setText("R : ");
ui->label_2->show();
ui->lineEdit_2->show();
ui->label_2->setText("ID : ");
ui->label_3->show();
ui->lineEdit_3->show();
ui->label_3->setText("X : ");
ui->label_4->show();
ui->lineEdit_4->show();
ui->label_4->setText("Y : ");
ui->Optional->hide();
ui->label_5->hide();
ui->label_6->hide();
ui->label_7->hide();
ui->lineEdit_5->hide();
ui->lineEdit_6->hide();
ui->lineEdit_7->hide();
ui->Gbox->show();
QPushButton *Road = new QPushButton("R");
QPushButton *Junction = new QPushButton("J");
QPushButton *Lane = new QPushButton("L");
QPushButton *Objects = new QPushButton("O");
QPushButton *Vehical = new QPushButton("V");
QPushButton *Fupdates = new QPushButton("F");
glay = new QGridLayout;
glay->addWidget(Road,0,0,1,1);
glay->addWidget(Junction,1,1,1,1);
glay->addWidget(Lane,2,0,1,1);
glay->addWidget(Objects,3,1,1,1);
glay->addWidget(Vehical,4,2,1,1);
glay->addWidget(Fupdates,5,3,1,1);
setLayout(glay);
ui->Gbox->setLayout(glay);
}
void test::on_2nd_clicked()
{
ui->label_1->show();
ui->lineEdit_1->show();
ui->label_1->setText("J: ");
ui->label_2->show();
ui->lineEdit_2->show();
ui->label_2->setText("ID : ");
ui->label_3->show();
ui->lineEdit_3->show();
ui->label_3->setText("X : ");
ui->label_4->show();
ui->lineEdit_4->show();
ui->label_4->setText("Y : ");
ui->Optional->show();
ui->Gbox->show();
QPushButton *A = new QPushButton("1");
QPushButton *B = new QPushButton("2");
QPushButton *C = new QPushButton("3");
QPushButton *D = new QPushButton("4");
QPushButton *E = new QPushButton("5");
QPushButton *F = new QPushButton("6");
vlay = new QVBoxLayout;
vlay->addWidget(A,1);
vlay->addWidget(B,0);
vlay->addWidget(C,0);
vlay->addWidget(D,1);
vlay->addWidget(E,1);
vlay->addWidget(F,0);
setLayout(vlay);
ui->Gbox->setLayout(vlay);
}
void test::on_Optional_stateChanged()
{
if(ui->Optional->isChecked())
{
ui->label_5->show();
ui->label_5->setText("P :");
ui->label_6->show();
ui->label_6->setText("S 1 :");
ui->label_7->show();
ui->label_7->setText("S 2 :");
ui->lineEdit_5->show();
ui->lineEdit_6->show();
ui->lineEdit_7->show();
}
else
{
ui->label_5->hide();
ui->label_6->hide();
ui->label_7->hide();
ui->lineEdit_5->hide();
ui->lineEdit_6->hide();
ui->lineEdit_7->hide();
}
}
答案 0 :(得分:0)
在每个方法中使用QLabel :: repaint(),并在为GBox设置新布局之前删除先前设置的布局,例如:
void test::on_2nd_clicked(){
ui->label_1->repaint();
ui->label_1->setText("J: ");
ui->label_2->repaint();
ui->label_2->setText("ID : ");
...
...
QPushButton *A = new QPushButton("1");
QPushButton *B = new QPushButton("2");
QPushButton *C = new QPushButton("3");
QPushButton *D = new QPushButton("4");
QPushButton *E = new QPushButton("5");
QPushButton *F = new QPushButton("6");
vlay = new QVBoxLayout;
vlay->addWidget(A,1);
vlay->addWidget(B,0);
vlay->addWidget(C,0);
vlay->addWidget(D,1);
vlay->addWidget(E,1);
vlay->addWidget(F,0);
setLayout(vlay);
delete glay;
ui->Gbox->setLayout(vlay);
}