Getting center of specific screen in virtual desktop qt - c++

Hey guys i need to get resolution of specific screen in virtual desktop under qt and move window around given screen. I tried
QRect screenSize = desktopWidget.availableGeometry(desktopWidget.screen(ui.monitorNumberComboBox->currentIndex()));
and now when i execute
void MyWindow::setCoordinates(int x, int y)
{
this->move((x-(this->width())/2),(y-(this->height()/2)));
//sets center of window on given coordinates
}
window->setCoordinates(screenSize.width()/2, screenSize.height()/2);
it works great but only for primary screen. Is there a possibility to use it for different screen, selected by index?

I think you need to call:
[..]
QPoint center = screenSize.center(); // Get the center of the screen rect.
window->setCoordinates(center.x(), center.y());

Related

Change Cursor Image While Hovering Over An Object Drawn At OpenGL And Displaying Object's Variables

I am currently working on a project that I use C++, OpenGL, Qt 5.9.2 and Microsoft Visual Studio Professional 2015 on a 64 bit Operating System, Windows 10 Pro.
I have a user interface that I have created and in that user interface, there is a QGLWidget, that I am using for draw processes, with other widgets like push buttons and a dockWidget. I have a class Ball and it has variables(distance(double) and angle(int)) that determines where an instance of that Ball is going to be drawn inside the QGLWidget. Ball class has got 2 more variables that is, id(int), model(String) and year(int) Every other draw process draws lines except Ball drawings.
Drawings are 2 dimensional.
Every Ball has the same color(rgb)!
First problem: I want to left click to one of the Ball instances and I want to display it's id, model and year at The dockWidget.
Second Problem: While doing the stuff that I have mentioned at the First Problem section. I want the cursor image to change while hovering above any of the Ball instances, and change back to default Windows mouse cursor while not.
I have created a function that checks if the MouseEvent is LeftClick:
void DisplayManager::mousePressEvent(QMouseEvent* ev) {
if (ev->buttons() & Qt::LeftButton) { // Balls Are Green
if(// CHECK IF THERE IS A BALL AT THE CLICKED COORDINATES) {
// DISPLAY THE X and Y OF THE BALL AT THE DOCK WIDGET
}
}
}
This is my initializeGL function: (DisplayManager is the name of my QGLWidget)
void DisplayManager::initializeGL() {
glEnable(GL_COLOR_MATERIAL); // Enables the changing of the draw color with glColor() functions
glColor3f(0.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1); //sets a black background 1 0 0 1
}
On the basis this is a Picking problem and there are several information about it at the internet but I am not using GLUT and I am not using any shader. So in the light of all these I was unable to find any effective solution or clue about how can I accomplish all that I want.
I would be really glad if someone could help me with at least one of these problems.
I have currently finished working with the project. I thought that I should provide an answer to my question in case someone with a similar problem comes across with my question in the future.
void DisplayManager::mousePressEvent(QMouseEvent* ev) {
// The processes that are being executed after a left mouse button click on the DisplayManager.
if (ev->buttons() & Qt::LeftButton) {
double aspectRatio = openGLWidgetWidth / openGLWidgetHeight;
int xOfClicked = ev->x() - (openGLWidgetWidth / 2);
int yOfClicked = - (ev->y() - (openGLWidgetHeight / 2));
// The variable to be used for calculating fault tolerance of the click event.
int distanceBetweenPressAndDraw = 0;
// Executes the processes inside for every ball in vector.
for (int i = 0; i < ballVector.length(); i++) {
// Calculates the screen coordinates of the i'th ball.
int rangeOfBallInScreenDistance = rangeToScreenDistance(ballVector.at(i).range);
double screenXOfBall = rangeOfBallInScreenDistance * sin(ballVector.at(i).degree * DEGTORAD);
double screenYOfBall = rangeOfBallInScreenDistance * cos(ballVector.at(i).degree * DEGTORAD);
// Calculates the distance between pressed position and the i'th ball according to the screen coordinates.
distanceBetweenPressAndDraw = sqrt(pow((screenXOfBall - xOfClicked), 2) + pow((screenYOfBall - yOfClicked), 2));
// Decides if the clicked position is a ball (considering the fault tolerance).
if (distanceBetweenPressAndDraw < 10) {
emit printXY(QPointF(xOfClicked, yOfClicked)); // Prints the screen coordinates of the clicked positions (At the Dock Widget inside Main Window).
}
}
}
}
This was the solution for my First Problem. I would be glad though if someone could answer my Second problem in a comment or answer somehow.

How to launch a window as a function of the size of the screen?

I am trying to find a solution to launch a window as a function of the size of the screen. I know there is the method resize() of the Gtk::Window but it is only pixels and not percent that's the problem.
Thank you !
You can get the screen width and height in pixels in a quick and dirty way like this:
#include <Windows.h> // Need this to get the screen resolution.
// Get the horizontal and vertical screen sizes in pixels:
static void GetDesktopResolution(int& horizontal, int& vertical) {
SetProcessDPIAware();
horizontal = GetSystemMetrics(SM_CXVIRTUALSCREEN);
vertical = GetSystemMetrics(SM_CYVIRTUALSCREEN);
}
For more advanced functionality, like dealing with multiple monitors, check out the link from the first comment on your question. The answers there are not just for OpenGL.

limit qwtPlotZoomer selection to canvas boundaries

The default behavior of qwtPlotZoomer seems to be that you can select a rectangle which exceeds the axis scale boundaries and canvas size, as seen in this screenshot from the realtime example:
I'd like to restrict the edges of the selection to be equal to the canvas or zoomRectboundaries. Is there any easy way to do this?
You could overload the move() method and bound the position to the contentsRect() of the canvas there ( or maybe better use the pickArea - what usually is the same ).
EDIT:
Based on Uwe's advice, I overloaded the move() function as follows, which prevents the picker's rect from being drawn off-screen.
void QZoomer::move(const QPoint& pos)
{
// bound the picker selection to the zoom base so you can't zoom off screen
QPoint boundedPos = pos;
if (boundedPos.x() > plot()->canvas()->contentsRect().right()) boundedPos.setX(plot()->canvas()->contentsRect().right());
if (boundedPos.x() < plot()->canvas()->contentsRect().left()) boundedPos.setX(plot()->canvas()->contentsRect().left());
if (boundedPos.y() < plot()->canvas()->contentsRect().top()) boundedPos.setY(plot()->canvas()->contentsRect().top());
if (boundedPos.y() > plot()->canvas()->contentsRect().bottom()) boundedPos.setY(plot()->canvas()->contentsRect().bottom());
QwtPlotPicker::move(boundedPos);
}
another way is to override the zoom function and bound the rect to the zoomBase. This limits the actual zoom to the zoom base, but still draws the picker outside the lines.
void Zoomer::zoom(const QRectF& rect)
{
// bound the zooming rect to the zoomBase dimensions
QRectF boundedRect = rect & zoomBase();
QwtPlotZoomer::zoom(boundedRect);
}

QT: Drawing Primitves to Follow Cursor

I am a bit new to QT. I have a separate Crosshair class that simply renders a crosshair using the QPainter and QPen. I used the paint() function and it does display the crosshairs at some position in the window. How can make the crosshairs follow the current mouse position?
This is my approach but I can't get it to work. I was following the VoidRealms tutorial.
void Crosshair::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
// i want to update the x and y position when the mouse moves
//x = mouse.x
//y = mouse.y
QGraphicsItem::mouseMoveEvent(event);
update();
}
This should do it for you:
this->setPos(event->x(), event->y());
There are also other helper functions available if you are doing a mapping to your scene when you are outside of QGraphicsSceneMouseEvent.
I described it here:
How to draw a point (on mouseclick) on a QGraphicsScene?
Hope that helps.

Relative mouse position of SDL_Surface

In my application I need to return the relative mouse position from an SDL_Surface, the problem is the mouse position that gets returned is relative to the SDL window and not the SDL_Surface. I guess my question is what is the easiest / most effective way of doing this. Any questions just ask. Thanks.
EDIT: Sorry I should have explained better, I have SDL_Surface* Surf_Display; on Surf_display there is an Image say its 1000 x 1000, So in order to see the image on a 600 x 600 window I have a camera that I can move around ( really its the surface that moves not the camera ) for instance to look right of the image I move the surface -1 left if that makes sense. So my problem is when I click my mouse on a part of the surface(image) my mouse returns the position that the mouse compared to where the cursor is in the window, what i'm wanting is so that it returns the position of the cursor compared to where it is on the surface(image)
I hope that better explains the situation. Thanks again
Just add(or subtract, depending on how you look at it) the offset to the mouse coordinates. So you're drawing the surface something like this:
SDL_Rect dest_rect = { -camera.x, -camera.y };
SDL_BlitSurface(image_surface, NULL, screen_surface, &dest_rect);
I don't know if you're using event based mouse handling, or if you're using SDL_GetMouseState, but either way, you would simply add camera.x and camera.y to the mouse position, for example:
int x, y;
SDL_GetMouseState(&x, &y);
x += camera.x;
y += camera.y;