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?
Forum Updated to NodeBB v4.3 + New Features

How to use same context menu on two QDateEdit objects?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 699 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mucip
    wrote on 27 Apr 2019, 17:20 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
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 27 Apr 2019, 17:53 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

      M 1 Reply Last reply 27 Apr 2019, 18:01
      2
      • C Christian Ehrlicher
        27 Apr 2019, 17:53

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

        M Offline
        M Offline
        Mucip
        wrote on 27 Apr 2019, 18:01 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
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 27 Apr 2019, 18:03 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

          M 1 Reply Last reply 27 Apr 2019, 18:11
          2
          • C Christian Ehrlicher
            27 Apr 2019, 18:03

            @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

            M Offline
            M Offline
            Mucip
            wrote on 27 Apr 2019, 18:11 last edited by
            #5

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

            Regards,
            Mucip:)

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 27 Apr 2019, 18:15 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

              M 1 Reply Last reply 27 Apr 2019, 18:29
              2
              • C Christian Ehrlicher
                27 Apr 2019, 18:15

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

                M Offline
                M Offline
                Mucip
                wrote on 27 Apr 2019, 18:29 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
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 27 Apr 2019, 18:30 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

                  7/8

                  27 Apr 2019, 18:29

                  • Login

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