Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mouse position is misaligned  (Read 722 times)

0 Members and 1 Guest are viewing this topic.

toara

  • Newbie
  • *
  • Posts: 3
    • View Profile
Mouse position is misaligned
« on: October 30, 2022, 12:33:59 pm »
Hello everyone

So, I followed this nice guide on how to get SFML and Qt to works together (https://github.com/SFML/SFML/wiki/Tutorial%3A-Integrating-SFML-into-Qt). Reason being: I want the menu bar but unfortunately SFML does not offer that :(. QFrame wasn't gonna cut it, so I used QMainWindow instead:

int main(int argc, char* argv[]) {
        QApplication application(argc, argv);

        QMainWindow window;

        // our sfml view widget
        QSfml qsfml;

        // throwing our sfml view widget into the window so it renders stuff
        window.setCentralWidget(&(qsfml));

        // the menu bars
        QMenu* gameOptions = window.menuBar()->addMenu("Options");
        gameOptions->addAction(qsfml.newGameAct);
        gameOptions->addAction(qsfml.exitGameAct);
        gameOptions->addSeparator();
        gameOptions->addAction(qsfml.closeWindowAct);

        window.resize(QSize(768, 570));
        qsfml.setSize(sf::Vector2u(768, 570));

        window.show();

        return application.exec();
}
 

And as I usually do, I used sf::Mouse::getPosition to grab the cursor position relative to the window, then I set the rectangle position to be the cursor's position, unfortunately it doesn't seem to like Qt very much and the rectangle gets misaligned by quite a bit

From my experience this usually happen when the window is squished or resized or something similar, but I have made sure that they are of the same size by calling resize and setSize on window and the SFML widget respectively

Also, one weird thing is that using 640x480 resolution for the SFML widget works absolutely fine, but I need a bigger window :\

I'm on linux with KDE Plasma X11, if that helps

Thanks a lot in advanced!
« Last Edit: October 30, 2022, 12:40:45 pm by toara »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Mouse position is misaligned
« Reply #1 on: October 31, 2022, 10:46:17 am »
The provided code only shows the basic Qt setup, so kind of hard to see what's actually going on.
Is the SFML "window" rendered at 0,0 inside the Qt window?

What you just use getPosition() without passing the window and get the Qt window position yourself and do the relative calculation in your own code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

toara

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Mouse position is misaligned
« Reply #2 on: November 03, 2022, 11:09:39 am »
Sorry for the complete lack of information and a slow response from my end, I didn't expect how busy I was gonna get :P

Anyway I have this bit of code responsible for getting the mouse position:

void QSfml::UpdateMousePosition() {
        this->mousePos = sf::Mouse::getPosition(*this);
}
 

And this bit responsible for drawing the rectangle:
void QSfml::Render() {
        this->tile.setPosition(this->mousePos.x, this->mousePos.y);

        this->draw(this->tile);
}
 

The result was as you can see from the line above

Quote
The provided code only shows the basic Qt setup, so kind of hard to see what's actually going on.
Since the code is rather similar to the official guide, I didn't think it was worth including, but just in case, I have uploaded the code to Github:
https://github.com/toaranotdev/llk

Quote
Is the SFML "window" rendered at 0,0 inside the Qt window?
Yes

Quote
What you just use getPosition() without passing the window and get the Qt window position yourself and do the relative calculation in your own code?
I'm too big of a noob to do that obviously XD

toara

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Mouse position is misaligned
« Reply #3 on: November 03, 2022, 11:55:01 am »
Worth noting thing I just discovered: the mouse position is completely correct. The only thing messing up here is probably SFML draw function... that's weird ??? Also the offset gets even worse the further I move my mouse away from the top left corner. I guess SFML and Qt doesn't really like each other

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Mouse position is misaligned
« Reply #4 on: November 07, 2022, 12:39:22 pm »
I guess the view might not be adjusted. So you'll have to make sure that the view matches the window size and isn't scaled wrong.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything