qt程序,Debug编译正常运行,Release运行时程序起不来,发现输出信息中有错误QTableWidget: cannot insert an item that is already owned by another QTableWidget提示。调试了两三天才解决。
之前以为这个错误肯定是QTableWidget有关的错误,由于运行时错误难以调试,只能全局搜索QTableWidget,QTableWidgetItem使用的地方,还好不算多,但仔细查看了最近的几次修改,发现并没有类似以下图片这种QTableWidgetItem重复插入的问题
这是因为在循环外创建的相同项目多次设置到同一表中。将代码更改如下才正确:
无奈只能逐段屏蔽修改代码,最终发现是因为同一UI部分代码想在不同机器显示成不同外观,分别写在两个函数,然后条件分支显示导致此问题,其实跟QTableWidget并无关,运行时报错QTableWidget也是很迷惑。
贴出出错代码
void QHemaxAL::InitialButtons()
{
QPixmap pixBk;
QString strPath = QDir::currentPath() + "/";
pixBk.load(strPath + "Image/Btn.jpg");
int nPixWidht = pixBk.width();
int nPixHeight = pixBk.height();
m_btnCount = new QUIButton(this);
connect(m_btnCount, &QPushButton::clicked, this, &QHemaxAL::OnBnCountClick);
QPixmap pixCount = pixBk.copy(QRect(0, 0, nPixWidht / 4, nPixHeight / 4));
pixCount = pixCount.scaled(QSize(64, 64));
QString strNormal2 = strPath + "Image/count_normal.png";
QString strHover2 = strPath + "Image/count_hover.png";
QString strClick2 = strPath + "Image/count_clicked.png";
QString strDisable2 = strPath + "Image/count_gray.png";
m_btnCount->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnCalibrate = new QUIButton(this);
QPixmap pixRemain = pixBk.copy(QRect(nPixWidht / 4, 0, nPixWidht / 4, nPixHeight / 4));
pixRemain = pixRemain.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/cal_normal.png";
strHover2 = strPath + "Image/cal_hover.png";
strClick2 = strPath + "Image/cal_clicked.png";
strDisable2 = strPath + "Image/cal_gray.png";
m_btnCalibrate->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
connect(m_btnCalibrate, &QPushButton::clicked, this, &QHemaxAL::OnBnCalibrateClick);
m_btnQc = new QUIButton(this);
connect(m_btnQc, &QPushButton::clicked, this, &QHemaxAL::OnBnQcClick);
QPixmap pixQc = pixBk.copy(QRect((nPixWidht * 2) / 4, 0, nPixWidht / 4, nPixHeight / 4));
pixQc = pixQc.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/qc_normal.png";
strHover2 = strPath + "Image/qc_hover.png";
strClick2 = strPath + "Image/qc_clicked.png";
strDisable2 = strPath + "Image/qc_gray.png";
m_btnQc->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnStatistic = new QUIButton(this);
connect(m_btnStatistic, &QPushButton::clicked, this, &QHemaxAL::OnBnStatisticClick);
QPixmap pixStatistic = pixBk.copy(QRect((nPixWidht * 3) / 4, 0, nPixWidht / 4, nPixHeight / 4));
pixStatistic = pixStatistic.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/statics_normal.png";
strHover2 = strPath + "Image/statics_hover.png";
strClick2 = strPath + "Image/statics_clicked.png";
strDisable2 = strPath + "Image/statics_gray.png";
m_btnStatistic->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnWorkSheet = new QUIButton(this);
connect(m_btnWorkSheet, &QPushButton::clicked, this, &QHemaxAL::OnBnWorkSheetClick);
QPixmap pixWorkSheet = pixBk.copy(QRect(0, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixWorkSheet = pixWorkSheet.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/work_sheet_normal.png";
strHover2 = strPath + "Image/work_sheet_hover.png";
strClick2 = strPath + "Image/work_sheet_clicked.png";
strDisable2 = strPath + "Image/work_sheet_gray.png";
m_btnWorkSheet->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnSet = new QUIButton(this);
connect(m_btnSet, &QPushButton::clicked, this, &QHemaxAL::OnBnSetClick);
QPixmap pixSet = pixBk.copy(QRect(nPixWidht / 4, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixSet = pixSet.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/set_normal.png";
strHover2 = strPath + "Image/set_hover.png";
strClick2 = strPath + "Image/set_clicked.png";
strDisable2 = strPath + "Image/set_gray.png";
m_btnSet->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnService = new QUIButton(this);
QPixmap pixService = pixBk.copy(QRect((nPixWidht * 2) / 4, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixService = pixService.scaled(QSize(64, 64));
connect(m_btnService, &QPushButton::clicked, this, &QHemaxAL::OnBnServiceClick);
strNormal2 = strPath + "Image/service_normal.png";
strHover2 = strPath + "Image/service_hover.png";
strClick2 = strPath + "Image/service_clicked.png";
strDisable2 = strPath + "Image/service_gray.png";
m_btnService->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnLog = new QUIButton(this);
QPixmap pixLog = pixBk.copy(QRect((nPixWidht * 3) / 4, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixLog = pixLog.scaled(QSize(64, 64));
connect(m_btnLog, &QPushButton::clicked, this, &QHemaxAL::OnBnLogClick);
strNormal2 = strPath + "Image/log_normal.png";
strHover2 = strPath + "Image/log_hover.png";
strClick2 = strPath + "Image/log_clicked.png";
strDisable2 = strPath + "Image/log_gray.png";
m_btnLog->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnSelfInspection = new QUIButton(this);
QPixmap pixSelfInspection = pixBk.copy(QRect(0, (nPixHeight * 2) / 4, nPixWidht / 4, nPixHeight / 4));
pixSelfInspection = pixSelfInspection.scaled(QSize(64, 64));
connect(m_btnSelfInspection, &QPushButton::clicked, this, &QHemaxAL::OnBnSelfInspectionClick);
strNormal2 = strPath + "Image/self_normal.png";
strHover2 = strPath + "Image/self_hover.png";
strClick2 = strPath + "Image/self_clicked.png";
strDisable2 = strPath + "Image/self_gray.png";
m_btnSelfInspection->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnClose = new QUIButton(this);
m_btnClose->LoadPixmap(strPath + "Image/closedefault.png", strPath + "Image/closehover.png", strPath + "Image/closeclick.png");
connect(m_btnClose, &QUIButton::clicked, this, &QHemaxAL::OnBnCloseClick);
m_btnExpand = new QUIButton(this);
QPixmap pixMap;
pixMap.load(strPath + "Image/xiaLa.png");
m_btnExpand->SetPixmap(pixMap);
connect(m_btnExpand, &QUIButton::clicked, this, &QHemaxAL::OnBnExpandClick);
m_btnMin = new QUIButton(this);
m_btnMin->LoadPixmap(strPath + "Image/smaller-default.png", strPath + "Image/smaller-hover.png", strPath + "Image/smaller-click.png");
connect(m_btnMin, &QUIButton::clicked, this, &QHemaxAL::OnBnMinClick);
m_btnLogout = new QUIButton(this);
m_btnLogout->LoadPixmap(strPath + "Image/btn-logout-default.png", strPath + "Image/btn-logout-hover.png", strPath + "Image/btn-logout-click.png");
connect(m_btnLogout, &QUIButton::clicked, this, &QHemaxAL::OnBnExpand1Click);
QFont font;
font.setFamily(QString::fromLocal8Bit("微软雅黑"));
font.setPixelSize(13);
QString styleLabel = QString("color:rgb(50, 50, 50)");
m_txtCount = new QLabel(this);
m_txtCount->setStyleSheet(styleLabel);
m_txtCount->setFont(font);
m_txtRemain = new QLabel(this);
m_txtRemain->setStyleSheet(styleLabel);
m_txtRemain->setFont(font);
m_txtQc = new QLabel(this);
m_txtQc->setStyleSheet(styleLabel);
m_txtQc->setFont(font);
m_txtStatistic = new QLabel(this);
m_txtStatistic->setStyleSheet(styleLabel);
m_txtStatistic->setFont(font);
m_txtWorkSheet = new QLabel(this);
m_txtWorkSheet->setStyleSheet(styleLabel);
m_txtWorkSheet->setFont(font);
m_txtSet = new QLabel(this);
m_txtSet->setStyleSheet(styleLabel);
m_txtSet->setFont(font);
m_txtService = new QLabel(this);
m_txtService->setStyleSheet(styleLabel);
m_txtService->setFont(font);
m_txtSelfInspection = new QLabel(this);
m_txtSelfInspection->setStyleSheet(styleLabel);
m_txtSelfInspection->setFont(font);
m_txtLog = new QLabel(this);
m_txtLog->setStyleSheet(styleLabel);
m_txtLog->setFont(font);
m_labelUserName = new QLabel(this);
m_labelUserName->setStyleSheet(styleLabel);
m_labelUserName->setFont(font);
m_txtUserName = new QLabel(this);
m_txtUserName->setStyleSheet(styleLabel);
m_txtUserName->setFont(font);
m_txtRunTime = new QLabel(this);
m_txtRunTime->setStyleSheet(styleLabel);
m_txtRunTime->setFont(font);
m_labelRunTime = new QLabel(this);
m_labelRunTime->setStyleSheet(styleLabel);
m_labelRunTime->setFont(font);
m_txtDateTime = new QLabel(this);
m_txtDateTime->setStyleSheet(styleLabel);
m_txtDateTime->setFont(font);
m_pFuncTips = new QLabel(this);//add by lm
m_pFuncTips->setStyleSheet(styleLabel);//add by lm
m_pFuncTips->setFont(font);//add by lm
}
void QHemaxAL::Initial53Buttons()
{
QPixmap pixBk;
QString strPath = QDir::currentPath() + "/";
QFont font;
font.setFamily(QString::fromLocal8Bit("微软雅黑"));
font.setPixelSize(13);
QString styleLabel = QString("color:rgb(50, 50, 50)");
m_btnClose = new QUIButton(this);
m_btnClose->LoadPixmap(strPath + "Image/closedefault.png", strPath + "Image/closehover.png", strPath + "Image/closeclick.png");
connect(m_btnClose, &QUIButton::clicked, this, &QHemaxAL::OnBnCloseClick);
m_btnExpand = new QUIButton(this);
QPixmap pixMap;
pixMap.load(strPath + "Image/xiaLa.png");
m_btnExpand->SetPixmap(pixMap);
connect(m_btnExpand, &QUIButton::clicked, this, &QHemaxAL::OnBnExpandClick);
m_btnMin = new QUIButton(this);
m_btnMin->LoadPixmap(strPath + "Image/smaller-default.png", strPath + "Image/smaller-hover.png", strPath + "Image/smaller-click.png");
connect(m_btnMin, &QUIButton::clicked, this, &QHemaxAL::OnBnMinClick);
m_btnLogout = new QUIButton(this);
m_btnLogout->LoadPixmap(strPath + "Image/btn-logout-default.png", strPath + "Image/btn-logout-hover.png", strPath + "Image/btn-logout-click.png");
connect(m_btnLogout, &QUIButton::clicked, this, &QHemaxAL::OnBnExpand1Click);
m_labelUserName = new QLabel(this);
m_labelUserName->setStyleSheet(styleLabel);
m_labelUserName->setFont(font);
m_txtUserName = new QLabel(this);
m_txtUserName->setStyleSheet(styleLabel);
m_txtUserName->setFont(font);
m_txtRunTime = new QLabel(this);
m_txtRunTime->setStyleSheet(styleLabel);
m_txtRunTime->setFont(font);
m_labelRunTime = new QLabel(this);
m_labelRunTime->setStyleSheet(styleLabel);
m_labelRunTime->setFont(font);
m_txtDateTime = new QLabel(this);
m_txtDateTime->setStyleSheet(styleLabel);
m_txtDateTime->setFont(font);
m_pFuncTips = new QLabel(this);
m_pFuncTips->setStyleSheet(styleLabel);
m_pFuncTips->setFont(font);
}
if (global.m_nAnalyzerType == EAnalyzerType::eHemax53)
{
Initial53Buttons();
}
else
{
InitialButtons();
}
}
控件声明为同一处,不同仪器下分别new出控件(部分相同控件),编译时不报错,运行时却出错了,最终修改为同一函数中new出相同部分,即不在两个函数里对同一控件进行new可正确运行
void InitialButtons()
{
QPixmap pixBk;
QString strPath = QDir::currentPath() + "/";
pixBk.load(strPath + "Image/Btn.jpg");
int nPixWidht = pixBk.width();
int nPixHeight = pixBk.height();
m_btnCount = new QUIButton(this);
connect(m_btnCount, &QPushButton::clicked, this, &QHemaxAL::OnBnCountClick);
QPixmap pixCount = pixBk.copy(QRect(0, 0, nPixWidht / 4, nPixHeight / 4));
pixCount = pixCount.scaled(QSize(64, 64));
QString strNormal2 = strPath + "Image/count_normal.png";
QString strHover2 = strPath + "Image/count_hover.png";
QString strClick2 = strPath + "Image/count_clicked.png";
QString strDisable2 = strPath + "Image/count_gray.png";
m_btnCount->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnCalibrate = new QUIButton(this);
QPixmap pixRemain = pixBk.copy(QRect(nPixWidht / 4, 0, nPixWidht / 4, nPixHeight / 4));
pixRemain = pixRemain.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/cal_normal.png";
strHover2 = strPath + "Image/cal_hover.png";
strClick2 = strPath + "Image/cal_clicked.png";
strDisable2 = strPath + "Image/cal_gray.png";
m_btnCalibrate->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
connect(m_btnCalibrate, &QPushButton::clicked, this, &QHemaxAL::OnBnCalibrateClick);
m_btnQc = new QUIButton(this);
connect(m_btnQc, &QPushButton::clicked, this, &QHemaxAL::OnBnQcClick);
QPixmap pixQc = pixBk.copy(QRect((nPixWidht * 2) / 4, 0, nPixWidht / 4, nPixHeight / 4));
pixQc = pixQc.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/qc_normal.png";
strHover2 = strPath + "Image/qc_hover.png";
strClick2 = strPath + "Image/qc_clicked.png";
strDisable2 = strPath + "Image/qc_gray.png";
m_btnQc->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnStatistic = new QUIButton(this);
connect(m_btnStatistic, &QPushButton::clicked, this, &QHemaxAL::OnBnStatisticClick);
QPixmap pixStatistic = pixBk.copy(QRect((nPixWidht * 3) / 4, 0, nPixWidht / 4, nPixHeight / 4));
pixStatistic = pixStatistic.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/statics_normal.png";
strHover2 = strPath + "Image/statics_hover.png";
strClick2 = strPath + "Image/statics_clicked.png";
strDisable2 = strPath + "Image/statics_gray.png";
m_btnStatistic->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnWorkSheet = new QUIButton(this);
connect(m_btnWorkSheet, &QPushButton::clicked, this, &QHemaxAL::OnBnWorkSheetClick);
QPixmap pixWorkSheet = pixBk.copy(QRect(0, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixWorkSheet = pixWorkSheet.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/work_sheet_normal.png";
strHover2 = strPath + "Image/work_sheet_hover.png";
strClick2 = strPath + "Image/work_sheet_clicked.png";
strDisable2 = strPath + "Image/work_sheet_gray.png";
m_btnWorkSheet->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnSet = new QUIButton(this);
connect(m_btnSet, &QPushButton::clicked, this, &QHemaxAL::OnBnSetClick);
QPixmap pixSet = pixBk.copy(QRect(nPixWidht / 4, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixSet = pixSet.scaled(QSize(64, 64));
strNormal2 = strPath + "Image/set_normal.png";
strHover2 = strPath + "Image/set_hover.png";
strClick2 = strPath + "Image/set_clicked.png";
strDisable2 = strPath + "Image/set_gray.png";
m_btnSet->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnService = new QUIButton(this);
QPixmap pixService = pixBk.copy(QRect((nPixWidht * 2) / 4, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixService = pixService.scaled(QSize(64, 64));
connect(m_btnService, &QPushButton::clicked, this, &QHemaxAL::OnBnServiceClick);
strNormal2 = strPath + "Image/service_normal.png";
strHover2 = strPath + "Image/service_hover.png";
strClick2 = strPath + "Image/service_clicked.png";
strDisable2 = strPath + "Image/service_gray.png";
m_btnService->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnLog = new QUIButton(this);
QPixmap pixLog = pixBk.copy(QRect((nPixWidht * 3) / 4, nPixHeight / 4, nPixWidht / 4, nPixHeight / 4));
pixLog = pixLog.scaled(QSize(64, 64));
connect(m_btnLog, &QPushButton::clicked, this, &QHemaxAL::OnBnLogClick);
strNormal2 = strPath + "Image/log_normal.png";
strHover2 = strPath + "Image/log_hover.png";
strClick2 = strPath + "Image/log_clicked.png";
strDisable2 = strPath + "Image/log_gray.png";
m_btnLog->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnSelfInspection = new QUIButton(this);
QPixmap pixSelfInspection = pixBk.copy(QRect(0, (nPixHeight * 2) / 4, nPixWidht / 4, nPixHeight / 4));
pixSelfInspection = pixSelfInspection.scaled(QSize(64, 64));
connect(m_btnSelfInspection, &QPushButton::clicked, this, &QHemaxAL::OnBnSelfInspectionClick);
strNormal2 = strPath + "Image/self_normal.png";
strHover2 = strPath + "Image/self_hover.png";
strClick2 = strPath + "Image/self_clicked.png";
strDisable2 = strPath + "Image/self_gray.png";
m_btnSelfInspection->LoadPixmap(strNormal2, strHover2, strClick2, strDisable2);
m_btnClose = new QUIButton(this);
m_btnClose->LoadPixmap(strPath + "Image/closedefault.png", strPath + "Image/closehover.png", strPath + "Image/closeclick.png");
connect(m_btnClose, &QUIButton::clicked, this, &QHemaxAL::OnBnCloseClick);
m_btnExpand = new QUIButton(this);
QPixmap pixMap;
pixMap.load(strPath + "Image/xiaLa.png");
m_btnExpand->SetPixmap(pixMap);
connect(m_btnExpand, &QUIButton::clicked, this, &QHemaxAL::OnBnExpandClick);
m_btnMin = new QUIButton(this);
m_btnMin->LoadPixmap(strPath + "Image/smaller-default.png", strPath + "Image/smaller-hover.png", strPath + "Image/smaller-click.png");
connect(m_btnMin, &QUIButton::clicked, this, &QHemaxAL::OnBnMinClick);
m_btnLogout = new QUIButton(this);
m_btnLogout->LoadPixmap(strPath + "Image/btn-logout-default.png", strPath + "Image/btn-logout-hover.png", strPath + "Image/btn-logout-click.png");
connect(m_btnLogout, &QUIButton::clicked, this, &QHemaxAL::OnBnExpand1Click);
QFont font;
font.setFamily(QString::fromLocal8Bit("微软雅黑"));
font.setPixelSize(13);
QString styleLabel = QString("color:rgb(50, 50, 50)");
m_txtCount = new QLabel(this);
m_txtCount->setStyleSheet(styleLabel);
m_txtCount->setFont(font);
m_txtRemain = new QLabel(this);
m_txtRemain->setStyleSheet(styleLabel);
m_txtRemain->setFont(font);
m_txtQc = new QLabel(this);
m_txtQc->setStyleSheet(styleLabel);
m_txtQc->setFont(font);
m_txtStatistic = new QLabel(this);
m_txtStatistic->setStyleSheet(styleLabel);
m_txtStatistic->setFont(font);
m_txtWorkSheet = new QLabel(this);
m_txtWorkSheet->setStyleSheet(styleLabel);
m_txtWorkSheet->setFont(font);
m_txtSet = new QLabel(this);
m_txtSet->setStyleSheet(styleLabel);
m_txtSet->setFont(font);
m_txtService = new QLabel(this);
m_txtService->setStyleSheet(styleLabel);
m_txtService->setFont(font);
m_txtSelfInspection = new QLabel(this);
m_txtSelfInspection->setStyleSheet(styleLabel);
m_txtSelfInspection->setFont(font);
m_txtLog = new QLabel(this);
m_txtLog->setStyleSheet(styleLabel);
m_txtLog->setFont(font);
m_labelUserName = new QLabel(this);
m_labelUserName->setStyleSheet(styleLabel);
m_labelUserName->setFont(font);
m_txtUserName = new QLabel(this);
m_txtUserName->setStyleSheet(styleLabel);
m_txtUserName->setFont(font);
m_txtRunTime = new QLabel(this);
m_txtRunTime->setStyleSheet(styleLabel);
m_txtRunTime->setFont(font);
m_labelRunTime = new QLabel(this);
m_labelRunTime->setStyleSheet(styleLabel);
m_labelRunTime->setFont(font);
m_txtDateTime = new QLabel(this);
m_txtDateTime->setStyleSheet(styleLabel);
m_txtDateTime->setFont(font);
m_pFuncTips = new QLabel(this);
m_pFuncTips->setStyleSheet(styleLabel);
m_pFuncTips->setFont(font);
}