I am learning Qt and create a small example like this.
I have read some suggested questions which relate to my problem here but right now they are not easy for me to understand.
This is my code, above function is calendar interaction, below function is for showing items:
SmallExample::SmallExample(QWidget *parent)
: QWidget(parent)
{
.........
connect(ui.tableWidget, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(calendar_clicked(QTableWidgetItem*)));
}
void SmallExample::calendar_clicked(QTableWidgetItem* tableitem)
{
int column = tableitem->column();
SmallExample::row = tableitem->row();
if (column == 2) {
if (!calendar) {
calendar = new QCalendarWidget();
}
calendar->setWindowTitle("Calendar");
calendar->setWindowModality(Qt::WindowModal);
calendar->show();
connect(calendar, SIGNAL(activated(const QDate&)), this, SLOT(date_selected(const QDate&)));
}
}
void SmallExample::date_selected(const QDate&)
{
QTableWidgetItem *itemcalendar = new QTableWidgetItem;
QIcon icon(":/icon/calendar.jpg");
itemcalendar->setIcon(icon);
SmallExample::ui.tableWidget->setItem(SmallExample::row, 0, itemcalendar);
QString text= SmallExample::calendar->selectedDate().toString("dd.MM.yyyy");
QTableWidgetItem *datetext = new QTableWidgetItem;
datetext->setText(text);
SmallExample::ui.tableWidget->setItem(SmallExample::row, 0, datetext);
SmallExample::calendar->close();
}
I know when datetext
is added, the itemcalendar
will be overwritten, so it does not appear any more. I want both of which will appear, but I don't know how to solve this. Thanks in advance!
update code:
void SmallExample::date_selected(const QDate&)
{
QTableWidgetItem *itemcalendar = SmallExample::ui.tableWidget->item(SmallExample::row, 2);
QIcon icon(":/icon/calendar.jpg");
itemcalendar->setIcon(icon);
QString date = SmallExample::calendar->selectedDate().toString("dd.MM.yyyy")
itemcalendar->setText(date);
}