Results 1 to 6 of 6

Thread: Button in OpenGL

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Button in OpenGL

    Hi guys, I'm trying to create buttons inside an OpenGL scene.

    I have created a class for the OpenGL window and my program opens up and displays my picture in OpenGL. I have drawn buttons on the picture and now I'm trying to get mouse click inputs recognised. I'm just using Qt's mousePressEvent to detect mouse clicks inside the OpenGL window and am using signals and slots to emit the location of the mouse click in a QPoint object.

    My other class is the Button class which I will use for other purposes (hence trying to do it this way rather then just having the whole implementation done in my OpenGL class).

    So anyway, here's my button class:

    button.cpp

    Qt Code:
    1. #ifndef BUTTON_H
    2. #define BUTTON_H
    3.  
    4. #include <QWidget>
    5. #include <QMessageBox>
    6. #include <QMouseEvent>
    7.  
    8. class Button : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit Button(QWidget *parent = 0);
    13.  
    14. int m_iXLocation;
    15. int m_iYLocation;
    16. int m_iWidth;
    17. int m_iHeight;
    18.  
    19. Button(int iXLocation, int iYLocation, int iWidth, int iHeight) : m_iXLocation(iXLocation), m_iYLocation(iYLocation), m_iWidth(iWidth), m_iHeight(iHeight)
    20. {}
    21.  
    22. void SetButtonParameters(int iXLocation, int iYLocation, int iHeight, int iWidth);
    23.  
    24. //virtual void mousePressEvent(QMouseEvent *event);
    25. void ButtonClickedCheck();
    26.  
    27. QPoint mousePosition;
    28.  
    29. signals:
    30. public slots:
    31. void LocationGrabber(QPoint Location);
    32. };
    33.  
    34. #endif // BUTTON_H
    To copy to clipboard, switch view to plain text mode 

    So as you can see... the button class the parameters which set the button dimensions and location and the only thing that it receives as input from the OpenGL window is the location of the mouse click... I haven't figured out the transformation from OpenGL coordinates to window coordinates yet, but I'm aware of it and will do so in due course.

    Anyway... where should I create the instances of the buttons? Inside the constructor of the OpenGL class? or inside the constructor of the class which houses the ui? I've put in in the latter, however for some reason the signals and slots aren't picking up the clicks form the OpenGL window and my debugging message boxes aren't popping up... well to quite frank, I can't even compile it as I'm the parameters I'm inputting into connect (in order to connect slots) aren't really working. I know this has a lot of Qt related stuff, but it's still more of an object oriented question and I hope you can look past my problems with the Qt components.

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. mainwindow::mainwindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::mainwindow)
    5. {
    6. Button Button1(0,0,50,50);
    7. mainwindow::connect(mainwindowWidget, SIGNAL(MouseClickLocation(QPoint MousePositionAtClick)), Button1, SLOT(LocationGrabber(QPoint Location)));
    8. ui->setupUi(this);
    9. }
    10.  
    11. mainwindow::~mainwindow()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Maybe I made a mistake in the connect function? Maybe I need to use references somewhere? And yes, I've included button.h in the header for mainwindow.cpp
    Last edited by Atomic_Sheep; 30th August 2012 at 14:02.

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Button in OpenGL

    your Button1 gets out of scope has the constructor exit. Try declaring it as a pointer.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Button in OpenGL

    Hi John, thanks, got it to work. Another question. I would like to create different types of buttons e.g. one that does something i.e. a function, one that outputs a value such as a letter or something... for this, I've written different initialiser lists. However I'm not sure whether this is enough to differentiate between the buttons upon instantiation. i.e. when I instantiate a function button, I want it to do a callback or send out a signal (Qt) that will then be received somewhere else and do something... so in this case all I want to do is specify that this button is a function button. However if I want to create a button that spits out a letter, I want to be able to assign a letter to that button upon instantiation/initialisation. Since the button has many different member variables, if I create a particular instance of a button, what happens to the unused class member variables? Do they acquire the default constructor values... do they remain uninitialised? Not sure if I've explained myself clearly enough.

    Also... another question... lets say I create an object, is it possible to somehow pass the actual name of the object without needing to implement an additional member variable inside the class and declaring the name of that object upon instantiation of that object?
    Last edited by Atomic_Sheep; 5th September 2012 at 10:42.

  4. #4
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Button in OpenGL

    Not sure if I understand your questions, but you can define diferent constructors to define diferent member initialization/function calls when instantiating. If you are coding lots of diferent buttons perhaps you should consider using derived class buttons, to get a cleaner code and cleaner logic. If you do that, for the name of the object, you can avoid member variable, using just one method, something like this:

    class ButtonA : public Button
    {
    public:
    ButtonA();
    virtual QString name() const{return "buttonA";}
    .........
    }
    You will not need to set the object name at construction time, and will be able to get it whenever you want it.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  5. #5
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Button in OpenGL

    Ah yes good point, I actually started doing this this way a few months back when I started this problem and have since forgotten that I did it this way. I'll look into it again. My concern was simply the fact that some initialiser lists don't address various member variables or functions and that's what I was worried about since my understanding is that the default constructor isn't called if you call an overloaded one.

  6. #6
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Button in OpenGL

    The method of using derived classes ended up being useful under a rather narrow set of circumstances. I'm trying to expand the functionality of my button class. Previously I created derived classes such as Button::AlphanumericButton and Button::NumericButton Button::Function which basically allowed me to assign a numerical or alphanumerical value to these, however I would like for my buttons to be able to do something not just store values. For Button::Function, I just kept adding a bunch of different function names under #define and then inside the classes that actually used the buttons I would basically create functions for each of the functionalities that I needed and used simple if statements:

    Qt Code:
    1. void MyApp1::FunctionMultiply()
    2. {
    3. if(cButton.Function() == MULTIPLY) //sorry not a programmer, cButton.Function() might not be right, haven't written code in a while, but basically I'm invoking the member variable that stores the function value as per defines.
    4. {
    5. //do multiply
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    and then another app that I'm using would have:

    Qt Code:
    1. void MyApp2::FunctionGetDatabaseValue()
    2. {
    3. if(cButton.Function() == GET_DB_VALUE)
    4. {
    5. //access database and get this value
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    So quite obviously the derived class would grow infinitely large based on the number of functions that I needed in my various programs that I would make using the button class. Question is, how to best ammend my button class to be able to incorporate such functionality i.e. assign a button to a function in any individual and unique program?

Similar Threads

  1. OpenGL Rotation with only left button
    By danielperaza in forum Qt Programming
    Replies: 14
    Last Post: 7th August 2014, 09:35
  2. Replies: 2
    Last Post: 26th April 2011, 11:44
  3. Replies: 6
    Last Post: 21st August 2010, 21:09
  4. Replies: 0
    Last Post: 6th December 2009, 00:41
  5. Qt with OpenGL ES for ARM9 - All OpenGL ES tests have failed!
    By vinpa in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 3rd December 2009, 10:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.