How can complicated user controls be drawn on C++? [closed] - c++

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Frameworks like Qt and GTK+ offer to developers some standard user controls like editboxes, comboboxes, etc. But many GUI-applications have more complicated user controls. For example, text editors have areas where user can input text and insert images or tables. Audio editors usually display waveforms of audiotracks, volume indicators, etc. My question is how such controls are drawn? Moreover, these controls usually can handle events - for example, tables in text editors can be selected, on right mouse button click some context menu pops up, etc. So, problem is not only to draw these controls, but also to make controls handle events. I have only idea to use Canvas or OpenGL, but I suspect that it would be difficult to implement events handling. May be, there are more simple ways to draw user interface?

This is a pretty vague question, so this will be a somewhat vague answer. In general, most of this work has already been done. For example, Qt already has a text view class, an image class, and a table class (I'm not very experienced with Qt, just going from general stuff I know). For the text editor you are talking about, you might derive a class from a Qt text editing widget which has a handler to accept the dragging of images and can draw them. Again, Qt already has the code to draw images and do all kinds of painting.
To draw an audio waveform, you would have some sort of box or widget (generally speaking) that would draw a vertical line for each sampling interval to represent the volume or amplitude there.
Yes, OpenGL would be overkill for what you are talking about here because you would need to somehow write your own event handling, whereas most GUI frameworks do that for you. Again, a rather vague answer for a vague question, but I hope it helps!

Related

How to check if the mouse was clicked over a certain area in the console window? [duplicate]

This question already exists:
Can you get the mouse position and if it is clicked in a console app? [duplicate]
Closed 3 years ago.
Say I was building a simple game and I had a menu with Play, Settings, Music etc. All laid out in the middle of the screen, how would I check if the mouse was over one of those areas and if the mouse clicked while over one of those areas?
Other people have asked similar things to this but I cannot find a working example or a clear solution to my question.
For clicking and mouse input, there are libraries/APIs that give user input (such as GLFW with Opengl). It really depends on what platform you're using. Then, you can convert your mouse XY position to a position relative to the window, and see whether or not its XY is greater than the bottom left XY of the GUI and less than the top right XY.
I am no expert in GUI in C++ because C++ GUI is not efficient. But to be making a clickable button, you may not want to do all those hard works. All you need to find is how to add a button from some libraries and bind that button to certain function. That’s how things work.
Additionally, you should have provided information regarding to what you have tried and the problem you are facing to give others less headaches

Tower of hanoi using Qt [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to create a GUI for a tower of Hanoi application using Qt. I Already wrote the C++ code using recursion but I am a total beginner when it comes to working with Qt and creating GUI's and have just started familiarizing myself with it by creating a simple calculator GUI but don't have a clear idea how to tackle the Tower of Hanoi GUI especially on how to create the disks and pegs graphically.
Any tips or pointers on where to start would be welcome. Thank you.
I suggest to take a look at QGraphicsScene. It will take a while to get familiar, but it is the proper Qt tool for handling these kind of things.
Basically, QGraphicsScene is an abstract representation of, well, a graphics scene. It is a virtual canvas object to which you can add graphics items (QAbstractGraphicsItems) with various shapes and positions. It can then be rendered in a specialized view class called QGraphicsView.
A good starting point is the '40000 chips' example in Qt Creator.
I can recommend 2 options:
One would be using an OpenGL canvas inside your window and have objects loaded into your 2D scene. And when you need to move or rotate the objects, you would just need to know about translate and rotate functionalities.
You wouldn't need to do this with modern OpenGL, as it might be overwhelming at the starts. But OpenGL with fixed pipeline (legacy version) would be quite easy to learn. However, I should mention that QT and OpenGL can be a little trouble to use together for a beginner due to QT.
The other would be the QPainter to load and draw 2D images (of components of towers). It wouldn't be as realistic as an OpenGL equavelent, but it would get the job done as well.
You can find the functions available via QPainter in the following link. It has almost everything you would need and to move a part, just render using the initial position values added to the offset.
http://doc.qt.io/qt-5/qpainter.html
I also recommend watching a few videos on Youtube about translation in computer graphics, to understand the logic behind this common functionality about moving things in a scene in amounts dependent on other conditions.

Simple OpenGL GUI Framework User Interaction Advice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm designing a simple GUI framework from scratch as a project, using OpenGL and nothing else external and need some advice on how I might implement user interaction.
Basically, I've a base class GUIItem from which all elements inherit. This gives each item some basic variables such as position, a vector to contain child elements as well as some basic functions for mouse movement and clicking.
All elements are setup as above, with their relevant member variables.
What I'm struggling with is how to implement user interaction properly. In my window manager I would create a new instance of an item, say GUIButton and call it button1. The window manager would, upon a click occurring, iterate through its list of elements and any child elements they may have, calculating a rectangular area around the object based on its coordinates, height and width, then running any "on click" function associated with said item, like change the value of textlabel1.
Firstly, is there a better way to do this calculation? It would work for rectangular elements but spherical objects and others would have a much larger erroneous area which could be clicked. Ideally I would check pixels but I've no real idea how that would be achieved. I've heard about but never used GLUT (my project only allows use of this for handling mouse/keyboard interaction though). Does GLUT provide anything to assist in this case?
My main issue is with handling what would occur when an "On click" event actually occurred. At the moment GUIButton for example, has an "On click" function built in, so as far as I can see, I'd have to do something like make it a virtual function, meaning that each new button I created would have to have its own class just to overwrite the "on click" function and each instance of a button would be an instance of a unique class that simply inherited off of GUIButton. This seems messy to me, as I've no idea where I would store all those classes, and it seems a lot of extra code. Would I be creating a button1.cpp and button1.h file?
Any advice on this really would be welcome as I'm new to C++, OpenGL and it's the first time I've been exposed to GUI programming and there's not a lot to go on when an existing GUI framework is the usual choice.
if you want something stupidly simple and fast then you could:
create shadow screen buffer containing ID/index/pointer instead of color
pre-render this buffer
Just render each of your visual component to it but instead coloring/texturing just fill in the ID/index/pointer of rendered component. Do not forget to clear this with some NULL first ... After this you should have mask of your components. You need to do this just once ...
On mouse events
you simply convert mouse coordinates to the shadow screen space and pick the value. If it is NULL then you clicked or whatever on empty area. If it contains ID instead update or call the callbacks for component ID. if you have a list of all components then ID can be the list index, otherwise use its actual pointer or encode in style (component_type, component_index). As you can see this is pretty fast O(1) item selection no matter how many components you have ... The shadow screen can have different resolution then your actual screen (to preserve memory).
This have pixel perfect mouse selection accuracy no matter the shape of your components without the need for nested component search loops.
[Notes]
As I did this stuff here are some hints:
create a window class containing configuration of your components for single screen. Programs have usually more screens with different set of components and doing dynamically the screens over and over again just because you switch page/screen sucks.
use separate list of components one list per component type.
create IDE editor for your windows see drag & drop example in C++ it might get handy for this. Add get,set functions controlled by string/enum or flag to easy obtain/change properties to make Object Inspector possible. Also this is how mine IDE looks like:
The window is saved from IDE directly as C++ code I can just copy to my App. This is the above example without the knob (forgot to save it):
//---------------------------------------------------------------------------
// OpenGL VCL window beg: win
win.grid.allocate(0);
win.grid.num=0;
win.scale.allocate(0);
win.scale.num=0;
win.button.allocate(0);
win.button.num=0;
win.knob.allocate(0);
win.knob.num=0;
win.scrollbar.allocate(3);
win.scrollbar.num=3;
win.scrollbar[0].x0=200.0;
win.scrollbar[0].y0=19.0;
win.scrollbar[0].xs=256.0;
win.scrollbar[0].ys=16.0;
win.scrollbar[0].fxs=8.0;
win.scrollbar[0].fys=19.0;
win.scrollbar[0].name="_vcl_scrollbar0";
win.scrollbar[0].hint="";
win.scrollbar[0].min=0.000;
win.scrollbar[0].max=1.000;
win.scrollbar[0].pos=0.000;
win.scrollbar[0].dpos=0.100;
win.scrollbar[0].horizontal=1;
win.scrollbar[0].style=0;
win.scrollbar[0].resize();
win.scrollbar[1].x0=200.0;
win.scrollbar[1].y0=45.0;
win.scrollbar[1].xs=256.0;
win.scrollbar[1].ys=16.0;
win.scrollbar[1].fxs=8.0;
win.scrollbar[1].fys=19.0;
win.scrollbar[1].name="_vcl_scrollbar1";
win.scrollbar[1].hint="";
win.scrollbar[1].min=0.000;
win.scrollbar[1].max=1.000;
win.scrollbar[1].pos=0.000;
win.scrollbar[1].dpos=0.100;
win.scrollbar[1].horizontal=1;
win.scrollbar[1].style=0;
win.scrollbar[1].resize();
win.scrollbar[2].x0=200.0;
win.scrollbar[2].y0=70.0;
win.scrollbar[2].xs=256.0;
win.scrollbar[2].ys=16.0;
win.scrollbar[2].fxs=8.0;
win.scrollbar[2].fys=19.0;
win.scrollbar[2].name="_vcl_scrollbar2";
win.scrollbar[2].hint="";
win.scrollbar[2].min=0.000;
win.scrollbar[2].max=1.000;
win.scrollbar[2].pos=0.000;
win.scrollbar[2].dpos=0.100;
win.scrollbar[2].horizontal=1;
win.scrollbar[2].style=0;
win.scrollbar[2].resize();
win.interpbox.allocate(0);
win.interpbox.num=0;
win.dblist.allocate(0);
win.dblist.num=0;
// OpenGL VCL window end: win
//---------------------------------------------------------------------------
Look at images here plotting real time Data on Oscillocope for some ideas (I got this working for both GDI and OpenGL)
It is better to use pixel units instead of OpenGL <-1,+1> screen units for better visual quality and editing comfort.

How to use touch screen keyboard for QLineEdit

I feel like this question must have been ask and answered a thousand times, but I can't seem to find it here. Sorry if I missed it!
I have developed a program using Qt on my desktop, where I sometime (rarely) have to input some text in a QLineEdit (typically a new username). It works fine, but now I want to run it on a computer with touch screen and be able to enter text using an on-screen keyboard. I've seen many post with related question (usually a bit less trivial) with answer that seem way too complex for such a common task. So my question is:
Isn't there a very basic way to show up the touch screen keyboard whenever the QLineEdit field is selected?
I'm wondering if this would maybe automatically happen with any regular tablet, but maybe not on those silly laptops (like the one I have) which do have a physical keyboard, but also a touch screen that can be collapsed onto the keyboard to then be used as a tablet.

Simple game graphics using Qt library [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I want to create a very simple game, something like this. In general I need several colors of squares, they should appear, disappear, move while you click on them. I have created a QDialog. In it I use MyDialog::mousePressEvent(QMouseEvent *event) function in order to get the coordinates where mouse was clicked. In general I need two more things to programm my game.
Put images in my QDialog
Move them
Remove them form my dialog
Could you give some sample code that adds an image into the dialog, moves it and destroys, please? What is the most convenient way of doing this?
Place and image in specific coordinates in QGraphicsScene
Functions is the code are useful for creating a simple game: item.setPos, scene.addItem ...