私はQtを学習していて、このような小さな例を作成しています。1つのセル内の異なる項目QTableWidget
私はここに私の問題に関連しますが、今、彼らは私が理解するために容易ではない、いくつかの提案の質問を読みました。
これは機能上、私のコードでは、関数の下に、カレンダーの相互作用である項目を示すためのものです:
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();
}
私はdatetext
が追加されたときに知っている、それはもはや表示されませんので、itemcalendar
は、上書きされます。私はどちらも出現したいが、これを解決する方法はわからない。前もって感謝します!
更新コード:
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);
}
あなたは*同じQTableWidgetItemにテキストとアイコンを設定しようとしましたか? – Frodon
@Frodon:はい、それは私がテーブルのようにしたいものです。しかし、私のコードによれば、最初のアイコンが設定されていますが、テキストが設定されているとアイコンを上書きするのでアイコンはそれ以上ありません。他の言葉で言えば、私のコードでは、私は1つの事をセルに設定することができます。 :( – amateur
あなたのコードでは、新しい 'QTableWidgetItem *'インスタンスによって '(SmallExample :: row、2) 'の項目を上書きします:' itemcalendar-> setText(date); ' – Frodon