Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. get the last QWidget actived

get the last QWidget actived

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
21 Posts 4 Posters 4.9k Views 2 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.
  • B Offline
    B Offline
    bozo6919
    wrote on last edited by
    #1

    Hi,

    I'm searching for a function who can give me the last widget activated.

    I call a widget activated a widget who has focus with the mouse, tab, or keyboard.
    For beginning, just with the mouse. A little example :
    0_1537285354050_presentationNucleusCreator.PNG
    In both squares, I have a QTabWidget who contain two tabs.
    In my class, I have a variable "currentLayout" and a static accessor for this variable. In this same class, I want to have a function, that change, when is necessary, the variable "currentLayout".

    I have already try focusInEvent and mousePressEvent but with these functions, they are called when I pressed a button of my mouse inside the square, but outdoor the content of the tab. If I press my mouse in the middle of the square, the function is not called.
    There is a solution?

    Sorry for my English ^^'

    1 Reply Last reply
    0
    • mrjjM mrjj

      @bozo6919
      Hi
      Should dynamic_cast not be qobject_cast ?

      B Offline
      B Offline
      bozo6919
      wrote on last edited by
      #6

      @mrjj
      Hi yes but I have some problems with qobject_cast :
      "Error C2338 qobject_cast requires the type to have a Q_OBJECT macro"
      But in my class, I have the macro :

      class ProjectManager : public QObject
      {
      	Q_OBJECT
      
      public:
      	// Some function...
      	template <class T>
      	T findParent(QObject *child)
      	{
      		T r = nullptr;
      
      		if (!child)
      			return nullptr;
      		QObject *parent = child->parent();
      
      		while (parent) {
      			r = qobject_cast<T>(parent);
      			if (r)
      				return r;
      			parent = parent->parent();
      		}
      		return r;
      	}
      };
      

      Sorry for my English ^^'

      JonBJ 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Would QApplication::focusWidget be what you are looking for ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bozo6919
          wrote on last edited by
          #3

          I thinks it's the same, I remembre when I used it before, and it has same problem : i have to press the mouse outside its content :/

          Sorry for my English ^^'

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bozo6919
            wrote on last edited by
            #4

            I found this signal : QApplication::focusChanged(QWidget *old, QWidget *new)
            And it's work for what I want to do.
            But, this function sends me the most little child of all my widgets. So I have implemented this function :

            template <class T>T findParent(QObject *child)
            {
            	T r = 0;
            
            	if (!child)
            		return nullptr;
            	QObject *parent = child->parent();
            	
            	while (parent) {
            		r = dynamic_cast<T> (parent);
            		if (r)
            			return r;
            		parent = parent->parent();
            	}
            	return r;
            }
            

            And it's working with "findParent<LayoutPanelTabber *>(new);"
            Thanks

            Sorry for my English ^^'

            mrjjM 1 Reply Last reply
            3
            • B bozo6919

              I found this signal : QApplication::focusChanged(QWidget *old, QWidget *new)
              And it's work for what I want to do.
              But, this function sends me the most little child of all my widgets. So I have implemented this function :

              template <class T>T findParent(QObject *child)
              {
              	T r = 0;
              
              	if (!child)
              		return nullptr;
              	QObject *parent = child->parent();
              	
              	while (parent) {
              		r = dynamic_cast<T> (parent);
              		if (r)
              			return r;
              		parent = parent->parent();
              	}
              	return r;
              }
              

              And it's working with "findParent<LayoutPanelTabber *>(new);"
              Thanks

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @bozo6919
              Hi
              Should dynamic_cast not be qobject_cast ?

              B 1 Reply Last reply
              2
              • mrjjM mrjj

                @bozo6919
                Hi
                Should dynamic_cast not be qobject_cast ?

                B Offline
                B Offline
                bozo6919
                wrote on last edited by
                #6

                @mrjj
                Hi yes but I have some problems with qobject_cast :
                "Error C2338 qobject_cast requires the type to have a Q_OBJECT macro"
                But in my class, I have the macro :

                class ProjectManager : public QObject
                {
                	Q_OBJECT
                
                public:
                	// Some function...
                	template <class T>
                	T findParent(QObject *child)
                	{
                		T r = nullptr;
                
                		if (!child)
                			return nullptr;
                		QObject *parent = child->parent();
                
                		while (parent) {
                			r = qobject_cast<T>(parent);
                			if (r)
                				return r;
                			parent = parent->parent();
                		}
                		return r;
                	}
                };
                

                Sorry for my English ^^'

                JonBJ 1 Reply Last reply
                0
                • B bozo6919

                  @mrjj
                  Hi yes but I have some problems with qobject_cast :
                  "Error C2338 qobject_cast requires the type to have a Q_OBJECT macro"
                  But in my class, I have the macro :

                  class ProjectManager : public QObject
                  {
                  	Q_OBJECT
                  
                  public:
                  	// Some function...
                  	template <class T>
                  	T findParent(QObject *child)
                  	{
                  		T r = nullptr;
                  
                  		if (!child)
                  			return nullptr;
                  		QObject *parent = child->parent();
                  
                  		while (parent) {
                  			r = qobject_cast<T>(parent);
                  			if (r)
                  				return r;
                  			parent = parent->parent();
                  		}
                  		return r;
                  	}
                  };
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @bozo6919
                  I don't know what the answer is and I don't do C++, but

                  r = qobject_cast<T>(parent);
                  "Error C2338 qobject_cast requires the type to have a Q_OBJECT macro"

                  This will be complaining about the Q_OBJECT on parent or on type T (not sure which, perhaps the latter) not on your class, won't it?

                  mrjjM 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @bozo6919
                    I don't know what the answer is and I don't do C++, but

                    r = qobject_cast<T>(parent);
                    "Error C2338 qobject_cast requires the type to have a Q_OBJECT macro"

                    This will be complaining about the Q_OBJECT on parent or on type T (not sure which, perhaps the latter) not on your class, won't it?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @bozo6919
                    Hi
                    Ah maybe it don't like templates as T don't exist yet.
                    So if static check, there is no way for it to know if contains Q_OBJECT
                    or not.

                    JonBJ 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @bozo6919
                      Hi
                      Ah maybe it don't like templates as T don't exist yet.
                      So if static check, there is no way for it to know if contains Q_OBJECT
                      or not.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #9

                      @mrjj said in get the last QWidget actived:

                      Ah maybe it don't like templates as T don't exist yet.

                      That's what I was wondering/suspecting....

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        bozo6919
                        wrote on last edited by bozo6919
                        #10

                        Yes, it's what I was suspecting too, but in my class, if I call qobject_cast with T = ContentPanelEditor *, who is contained in the same project and have the macro Q_OBJECT, I have the same error...

                        Edit:
                        No, I misread. ^^' after the qobject_cast on my ContentPanelEditor, I have another cast for a class in another project. So he doesn't know about this class.
                        This other class is contained in a dll. There is a way to apply qobject_cast on it?

                        Sorry for my English ^^'

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          Hi,

                          Can you give more details about that chain of casts ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          B 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Hi,

                            Can you give more details about that chain of casts ?

                            B Offline
                            B Offline
                            bozo6919
                            wrote on last edited by
                            #12

                            Hi,

                            My program has two projects :

                            • one is a DLL.
                            • the other an EXE.
                              In my EXE, I have to apply a qobject_cast on a class who is contained in my DLL.
                              But if I put in this class (in the DLL) the macro Q_OBJECT, my IDE (visual studio 2017) cannot compile the project with an error of linking.

                            Sorry for my English ^^'

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              Is your .dll project also managed with Qt ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              B 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                Is your .dll project also managed with Qt ?

                                B Offline
                                B Offline
                                bozo6919
                                wrote on last edited by
                                #14

                                Yes, theses projects was made with Qt

                                Sorry for my English ^^'

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  Then there should be no problem with qobject_cast.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • B Offline
                                    B Offline
                                    bozo6919
                                    wrote on last edited by
                                    #16

                                    Yes, but I have problems, and I don't know yet how to resolve them

                                    Sorry for my English ^^'

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #17

                                      What exact problems ?

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • B Offline
                                        B Offline
                                        bozo6919
                                        wrote on last edited by
                                        #18

                                        Hi, sorry for my late

                                        This problem for example :

                                        #pragma once
                                        
                                        #include <QtCore/qobject.h>
                                        
                                        class __declspec(dllexport) LayoutManager : public QObject
                                        {
                                        	Q_OBJECT
                                        
                                        public:
                                        	template <class T>T findParent(QObject *child)
                                        	{
                                        		T r = nullptr;
                                        
                                        		if (!child)
                                        			return nullptr;
                                        		QObject *parent = child->parent();
                                        
                                        		while (parent) {
                                        			r = qobject_cast<T>(parent);
                                        			if (r)
                                        				return r;
                                        			parent = parent->parent();
                                        		}
                                        		return r;
                                        	}
                                        };
                                        

                                        And I have this error message :
                                        Error : C2338 : qobject_cast requires the type to have a Q_OBJECT macro

                                        But the macro is here. So instead of qobject_cast, I have to use dynamic_cast

                                        Sorry for my English ^^'

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #19

                                          What type are you looking for ?

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0
                                          • B Offline
                                            B Offline
                                            bozo6919
                                            wrote on last edited by
                                            #20

                                            This is a type who doesn't exist in this project. I use this function in another project

                                            Sorry for my English ^^'

                                            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