Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use same context menu on two QDateEdit objects?

How to use same context menu on two QDateEdit objects?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 647 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MucipM Offline
    MucipM Offline
    Mucip
    wrote on last edited by
    #1

    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:)

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why can't you call the same slot for the second QDateEdit?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      MucipM 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        Why can't you call the same slot for the second QDateEdit?

        MucipM Offline
        MucipM Offline
        Mucip
        wrote on last edited by
        #3

        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:)

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mucip said in How to use same context menu on two QDateEdit objects?:

          QPoint globalPos = ui->dateIslem->mapToGlobal(pos);

          See https://doc.qt.io/qt-5/qobject.html#sender

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          MucipM 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            @Mucip said in How to use same context menu on two QDateEdit objects?:

            QPoint globalPos = ui->dateIslem->mapToGlobal(pos);

            See https://doc.qt.io/qt-5/qobject.html#sender

            MucipM Offline
            MucipM Offline
            Mucip
            wrote on last edited by
            #5

            @Christian-Ehrlicher ,
            It looks that sender doesn't have "->mapToGlobal(pos)"...

            Regards,
            Mucip:)

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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."

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              MucipM 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                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."

                MucipM Offline
                MucipM Offline
                Mucip
                wrote on last edited by
                #7

                @Christian-Ehrlicher ,
                Thanks. I used below code:

                QDateEdit* trh = qobject_cast<QDateEdit*>(sender());
                QPoint globalPos = trh->mapToGlobal(pos);
                

                Work like a charm.

                Regards,
                Mucip:)

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  If the issue is fixed please mark the thread as solved, thx.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved