Solved How to use same context menu on two QDateEdit objects?
-
Hi,
I have two DateEdit in same window. Unfotunatelly we can not set empty date on this QDateEdit object and I really wonder why?!... :(I plant use contextmenu for these DateEdits "Reset Date" which is reset date to 01.01.2000 and "Today" which is set date to today's date.
I use context menu like below:
//Ters Tuş menüsü Tarih İşlem... ui->dateIslem->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->dateIslem, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowContextMenuTarih(const QPoint&)));
And the function is:
void BakimOnarim::ShowContextMenuTarih(const QPoint &pos) { QPoint globalPos = ui->dateIslem->mapToGlobal(pos); QMenu myMenu; myMenu.addAction("Reset Date"); myMenu.addAction("Today"); QAction* selectedItem = myMenu.exec(globalPos); // Hiçbir şey tıklanmazsa hata vermesin diye... :) if (selectedItem == nullptr) return; if (selectedItem->text() == "Reset Date" ) { ui->dateIslem->setDate(QDate::fromString("2000-01-01","yyyy-MM-dd")); } else if (selectedItem->text() == "Today" ) { ui->dateIslem->setDate(QDate::currentDate()); }; }
But the problem is I have two or maybe more DateEdit on the same form. Well I want to use same context menu on many deteEdits!
I need to use SENDER like specification which is target to sender object. And sender object need to be use same context menu?!
Any help?!...
P.S.: I wish Qt would permit empty date do DateEdit. :(
Regards,
Mucip:) -
Why can't you call the same slot for the second QDateEdit?
-
Hi @Christian-Ehrlicher ,
But there is belowed line in the context menu slot:QPoint globalPos = ui->dateIslem->mapToGlobal(pos);
Well How can I change to dateIslem, dateZaman, dateBitis ...
Or maybe I lost something?!...
Regards,
Mukcip:) -
@Mucip said in How to use same context menu on two QDateEdit objects?:
QPoint globalPos = ui->dateIslem->mapToGlobal(pos);
-
@Christian-Ehrlicher ,
It looks that sender doesn't have "->mapToGlobal(pos)"...Regards,
Mucip:) -
No, QObject has no mapToGlobal() but sender returns a (QObject -) pointer to your QDateEdit object so you can cast it as described in the documentation:
"Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. The pointer is valid only during the execution of the slot that calls this function from this object's thread context." -
@Christian-Ehrlicher ,
Thanks. I used below code:QDateEdit* trh = qobject_cast<QDateEdit*>(sender()); QPoint globalPos = trh->mapToGlobal(pos);
Work like a charm.
Regards,
Mucip:) -
If the issue is fixed please mark the thread as solved, thx.