I'm trying to center my QDialog.
Here's a code I used:
QRect screenGeometry = QApplication::desktop()->screenGeometry();
int x = (screenGeometry.width() - this->width()) / 2;
int y = (screenGeometry.height() - this->height()) / 2;
this->move(x, y);
But I didn't get my dialog at an appropriate posistion. When I printed values of a dialog's width and height I noticed that they were much smaller than a real ones. To check if something was working in a wrong way I changed it's geometry:
this->setGeometry(100,100,this->width(),this->height());
And my dialog shrank...
Can someone tell me what's going on?
QRect screenGeometry = QApplication::desktop()->screenGeometry();
QRect windowRect = rect();
First get a copy of your own window rect.
windowRect.moveCenter(screenGeometry.center());
Move the copy to the center of the screen's rect.
move(windowRect.topLeft());
Perform the actual move: set your windows top left point to the calculated top left point. No resize is necessary.
Related
I'm writing a program where I need to create a QLabel by code, instead of drag and drop, but I'm having problems positioning the QLabel as I want, this is the code I have:
if(ui->WorksList->currentItem()->text() == "Work1")
{
ui->InformationLabel->show();
QLabel *label = new QLabel(this);
label->show();
label->setText("Extraction");
label->setMinimumWidth(100);
int x = 2000;
x = label->geometry().x();
int y = 2000;
y = label->geometry().y();
}
With this piece of code my QLabel do not move from the top left corner.
Thank you
Your posted code doesn't move anything; as one of the comments said, you have to use the move method to move the label. If it doesn't show up, then it's either off screen or covered up by something else, or both. The coordinates for the move method are in the parent widget's coordinates, not the screen, and not the window. You've parented this QLabel to this, so the coordinates are relative to the top left corner of this, whatever that is.
Your code doesn't show what this is, nor any mention of how large the area of this is, so it's impossible for us to tell what coordinates might be reasonable.
I'm building a very simple image editor on Qt creator.I have my image displayed on a QGraphicsView and i want to give the user the ability to zoom in and out by a pushbutton.
I've searched a lot and found how to zoom in and out through the mouse wheel.As i am very new to Qt i can't adjust it to the pushbutton because i don't understand everything clearly.
I' ve tried this(without understanding completely what i'm doing)but the result isn't the wanted.It zooms in only once and quite abruptly.I want a smoother zoom and as many times as i want.
void MainWindow::on_pushButton_clicked(){
QMatrix matrix;
ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
matrix.scale(1.0,1.0);
ui->graphicsView->setMatrix(matrix);
ui->graphicsView->scale(1,-1);
}
I would be very grateful if you guys can help
Below is how I implemented zooming in my subclass of QGraphicsView. Note that you'd need to pass in different values of "zoom" to get different magnifications as the zoom factor is an absolute value, not a relative one.
(The optMousePos argument can be set to point to a QPoint indicating the spot that should be the central-point of the zoom transformation, or it can be left NULL if you don't care about that. I use it because I zoom in and out based on the user turning the wheel in his mouse, and when doing that, the user usually wants to zoom in towards the point where his mouse point is currently positioned, rather than in towards the center of the graphics area)
qreal _zoom = 0.0;
[...]
void MyQGraphWidgetSubclass :: SetZoomFactor(qreal zoom, const QPoint * optMousePos)
{
if ((zoom != _zoom)&&(zoom >= 0.02f)&&(zoom <= 1000000.0f))
{
QPointF oldPos;
if (optMousePos) oldPos = mapToScene(*optMousePos);
// Remember what point we were centered on before...
_zoom = zoom;
QMatrix m;
m.scale(_zoom, _zoom);
setMatrix(m);
if (optMousePos)
{
const QPointF newPos = mapFromScene(oldPos);
const QPointF move = newPos-*optMousePos;
horizontalScrollBar()->setValue(move.x() + horizontalScrollBar()->value());
verticalScrollBar()->setValue(move.y() + verticalScrollBar()->value());
}
}
}
void MyQGraphWidgetSubclass :: wheelEvent(QWheelEvent* event)
{
QPoint pos = event->pos();
SetZoomFactor(_zoom*pow(1.2, event->delta() / 240.0), &pos);
event->accept();
}
I am having a listbox where I set both the properties i.e, vertical and horizontal scroll to true.I am able to get vertical scroll bar but not able to get horizontal scroll bar when added a lengthy string.
Can anyone please let me know how get horizontal scroll bar for a listbox.
Adding this piece of code in OnInitDialog resolved my issue.
BOOL OnInitDialog()
{
CString str;
CSize sz;
int dx = 0;
CDC* pDC = m_listbox.GetDC();
for(int i=0; i < m_listbox.GetCount();i++)
{
m_listbox.GetText(i,str);
sz = pDC->GetTextExtent(str);
if(sz.cx > dx)
dx = sz.cx;
}
m_listbox.ReleaseDC(pDC);
if(m_listbox.GetHorizontalExtent() < dx )
{
m_listbox.SetHorizontalExtent(dx);
ASSERT(m_listbox.GetHorizontalExtent() == dx);
}
return TRUE;
}
You have to specify the horizontal scroll extent (max width in pixels). Do that by calling CListBox::SetHorizontalExtent.
In MFC, I had a listbox that was too big and extended past the right border of the window containing it. Once I made the listbox x dimension fit inside the window, the scrollbar started working properly again.
For some reason if the listbox is too big, Windows did not handle the scrollbar visibility properly as a side-effect of the sizing.
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());
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;