void SpecData::clear()
{
table = new QTableWidget(this);
table->setColumnCount(56);
table->setRowCount(10);
//table->resize(1366, 200);
QStringList headers;
headers << "400nm" << "410nm" << "420nm" << "430nm" << "440nm" << "450nm" << "460nm" << "470nm" << "480nm" << "490nm"
<< "500nm" << "510nm" << "520nm" << "530nm" << "540nm" << "550nm" << "560nm" << "570nm" << "580nm" << "590nm"
<< "600nm" << "610nm" << "620nm" << "630nm" << "640nm" << "650nm" << "660nm" << "670nm" << "680nm" << "690nm"
<< "700nm" << "710nm" << "720nm" << "730nm" << "740nm" << "750nm" << "760nm" << "770nm" << "780nm" << "790nm"
<< "800nm" << "810nm" << "820nm" << "830nm" << "840nm" << "850nm" << "860nm" << "870nm" << "880nm" << "890nm"
<< "900nm" << "910nm" << "920nm" << "930nm" << "940nm" << "950nm";
table->setHorizontalHeaderLabels(headers); //设置行标题,列标题不用设置,会自动排序
table->horizontalScrollBar()->setStyleSheet("QScrollBar{background:transparent; height:10px;}"
"QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
table->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent; width: 10px;}"
"QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");<span style="white-space:pre"> </span>//设置滚动条样式
}
displaySplitter->setStretchFactor(0,0);
设置拉伸因子,设置了之后table表格确实可以跟着大框架走了,也就是自适应。
void MainWindow::createSpecDataDispWidget()
{
specDataDispLabel = new SpecData();
specDataDispLabel->table;//->clear()
int location = 356;
for (int column = 0; column < 33; ++column)
{
QTableWidgetItem *item0;
item0 = new QTableWidgetItem;
double m = imageData[location][column];
QString txt = QString("%1").arg(m); //把数字十进制地转换成string型
item0->setText(txt);
specDataDispLabel->table->setItem(0, column, item0); //把txt内容填进表格里
}
// specDataDispLabel->setText(tr("(%1,%2)").arg(location_x).arg(location_y));
}
这里面有没有table很重要,上下要一致,否则就会出现两个表格 ,另外如果这样使用的话,就不能把table设置成private类型,应该是public型的
上下一致就是指这两句:
specDataDispLabel->table;
specDataDispLabel->table->setItem(0, column, item0); //把txt内容填进表格里
void MainWindow::resizeEvent(QResizeEvent *event)
{
float fixedRatio = 0.5*camera->imgGrey->height()/camera->imgGrey->width();
camDispTWidget->setFixedWidth(0.5*displayTopSplitter->width());
camDispTWidget->setFixedHeight(camDispTWidget->width()*fixedRatio*2);
specAnaImgTWidget->setFixedHeight(camDispTWidget->width()*fixedRatio*2);
specDataDispLabel->table->setFixedWidth(displayTopSplitter->width());
specDataDispLabel->table->setFixedHeight(displaySplitter->height());
}
这段话配合上面那个拉伸因子的语句,使得表格才真正地好看了,以前都是表格下方总会有一块空白的地方,也不知道究竟是为什么。
至此,已经算是非常成功了,但是有一个最重要的问题还没有解决,就是滚动条会在页面最大化之后消失,虽然点击你能看到的最右面一个表格,他也可以自己显示再右边一项,但是横向的滚动条就是找不到,应该是因为页面比较大,程序觉得视口大小没有超出窗口部件(也就是表格)大小,所以就不显示滚动条,于是我尝试用强制显示的方法,也就是QScrollArea,但是表格QTableWidget他自带ScrollBar,所以应该只能用初始化Scrollbar的方法解决,当然,我还没解决掉,就发现最主要问题不在于这些了,这是后话,会在下一章介绍。
参考Qt的那本书,强制显示是要用语句:scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);(如果有拼错的自己改)