【发布时间】:2019-09-24 08:38:38
【问题描述】:
我有一个包含 3 列的 QTableWidget。
前两列存储一个QDateTimeEdit 项目。
第三个存储一个QSpinBox,它应该列出此行中两个QDateTimeEdit 值之间的持续时间。
如何连接QDateTimeEdit 的信号以自动更新QSpinBox 中的持续时间,以防日期时间发生更改?
...
for (int i_row = 0; i_row < 100; ++i_row){
QTableWidget *t = ui->tableWidget;
QDateTimeEdit *start = new QDateTimeEdit();
QDateTimeEdit *end = new QDateTimeEdit();
t->setCellWidget(i_row,0,start);
t->setCellWidget(i_row,1,end);
QSpinBox *sp = new QSpinBox();
sp->setReadOnly(true);
t->setCellWidget(i_row,2,sp);
connect(start, SIGNAL(dateTimeChanged(const QDateTime &)), this, SLOT(adjustDuration()));
connect(end, SIGNAL(dateTimeChanged(const QDateTime &)), this, SLOT(adjustDuration()));
}
与插槽:
void mainWindow::adjustDuration()
{
QDateTimeEdit *s = qobject_cast<QDateTimeEdit *>(sender());
// How do I get row number of the sender within QTableWidget in order to be able to access proper 2nd QDateTimeEdit and QSpinBox?
// Simplified speaking: I would like to get the value i_row from the code before
}
我想通过使用->parent() 函数有可能吗?
【问题讨论】:
-
但是你的行和列不是静态的吗? IE。您的小部件始终具有预定义的行/列值。为什么需要计算它们?
-
不,用户可以在 GUI 中更改 QDateTimeEdit 的值。只有旋转框是
readOnly -
该值可以更改,但
QDateTimeEdit的位置在QTableWidget中保持不变,如果我没记错的话。因此,@vahancho 建议可能是解决您的问题的一种直接方法。 -
我已经更新了这个例子。就我而言,这三个项目有很多行,我需要从哪一行中获取哪个项目正在发送信号,以便能够选择正确的对应项(同一行中的第二个
QDateTimeEdit)来计算和更新正确的QSpinBox -
为什么不将
dateTimeChanged信号连接到捕获start、end和sp的lambda?那么你就可以随心所欲了。
标签: c++ qt signals-slots