在构造函数中,为tableWidget添加类容,无法正常显示,如:
Show_history_3D::Show_history_3D(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_dataHandleCore = new DataHandleCore();
small_window = new PersonInfoRec();
connect(m_dataHandleCore, SIGNAL(signalSetNewPerson(st_PERSON_INFO*)), this, SLOT(NewPerson(st_PERSON_INFO*)));
connect(m_dataHandleCore, SIGNAL(signalSetRegisterInfo(st_PERSON_INFO*)), this, SLOT(SetRegisterInfo(st_PERSON_INFO*)));
connect(m_dataHandleCore, SIGNAL(signalSetRecognizeInfo(st_PERSON_INFO*)), this, SLOT(SetRecognizeInfo(st_PERSON_INFO*)));
int regId = qRegisterMetaType<st_PERSON_INFO*>("st_PERSON_INFO*");
if (false == QMetaType::isRegistered(regId))
{
WS_ERROR("register message type AlarmData fail\n");
}
connect(ui.tableWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(getInfo(int, int)));
InitTableWidget();
WS_INFO("initialize interface success");
ui.tableWidget->insertRow(0);
QLabel *label;
ui.tableWidget->setRowHeight(0, 70);
label = new QLabel(QString::fromLocal8Bit("中文"));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 0, label);
label = new QLabel(QString::fromLocal8Bit("123"));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 1, label);
label = new QLabel(QString::fromStdString("123"));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 2, label);
label = new QLabel(QString::fromLocal8Bit("123"));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 3, label);
string time = "20191212114532456";
convert_time(time);
label = new QLabel(QString::fromStdString(time));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 4, label);
}
而在构造函数执行完毕之后,在成员函数中为其添加内容,就可以成功显示。
如:
void Show_history_3D::NewPerson(st_PERSON_INFO* person)
{
//插入第一行
ui.tableWidget->insertRow(0);
QLabel *label;
ui.tableWidget->setRowHeight(0, 70);
//QLabel *label = new QLabel(QString::fromStdString(person.deviceId));
//label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//ui.tableWidget->setCellWidget(0, 0, label);
label = new QLabel(QString::fromLocal8Bit(person->name.c_str()));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 0, label);
label = new QLabel(QString::fromLocal8Bit(person->sex.c_str()));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 1, label);
label = new QLabel(QString::fromStdString(person->identifyCard));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 2, label);
label = new QLabel(QString::fromLocal8Bit(person->national.c_str()));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 3, label);
convert_time(person->PassTime);
label = new QLabel(QString::fromStdString(person->PassTime));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setCellWidget(0, 4, label);
//将2维消息存下来
person_base_info save;
save_base.push_front(save.get_info(person));
if (ui.tableWidget->rowCount() > 50)
{
ui.tableWidget->removeRow(50);
save_base.pop_back();
WS_INFO("Delete records that exceed 50");
}
}
原因暂时不知道,知道后补充