Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:13:28+00:00 2026-05-31T22:13:28+00:00

I’m messing around with QGraphicsView and QGraphicsScene to create a Tic Tac Toe clone.

  • 0

I’m messing around with QGraphicsView and QGraphicsScene to create a Tic Tac Toe clone. I add some QGraphicsLineItems to my scene and override the resizeEvent method of the Widget that contains the view, so that when the window is resized, the view and its contents are scaled appropriately. This works fine, except for the first time that I run the program:

Incorrect scaling

Once I resize the window by any amount, the scene is scaled correctly:

Correct scaling

Here’s the code:

main.cpp:

#include <QtGui>

#include "TestApp.h"

int main(int argv, char **args)
{
    QApplication app(argv, args);

    TestApp window;
    window.show();

    return app.exec();
}

TestApp.h:

#ifndef TEST_APP_H
#define TEST_APP_H

#include <QtGui>

class TestApp : public QMainWindow
{
    Q_OBJECT
public:
    TestApp();
protected:
    void resizeEvent(QResizeEvent* event);

    QGraphicsView* view;
    QGraphicsScene* scene;
};

#endif

TestApp.cpp:

#include "TestApp.h"

TestApp::TestApp()
: view(new QGraphicsView(this))
, scene(new QGraphicsScene(this))
{
    resize(220, 220);
    scene->setSceneRect(0, 0, 200, 200);

    const int BOARD_WIDTH = 3;
    const int BOARD_HEIGHT = 3;
    const QPoint SQUARE_SIZE = QPoint(66, 66);

    const int LINE_WIDTH = 10;
    const int HALF_LINE_WIDTH = LINE_WIDTH / 2;
    QBrush lineBrush = QBrush(Qt::black);
    QPen linePen = QPen(lineBrush, LINE_WIDTH);

    for(int x = 1; x < BOARD_WIDTH; ++x)
    {
        int x1 = x * SQUARE_SIZE.x();
        scene->addLine(x1, HALF_LINE_WIDTH, x1, scene->height() - HALF_LINE_WIDTH, linePen);
    }
    for(int y = 1; y < BOARD_HEIGHT; ++y)
    {
        int y1 = y * SQUARE_SIZE.y();
        scene->addLine(HALF_LINE_WIDTH, y1, scene->width() - HALF_LINE_WIDTH, y1, linePen);
    }

    view->setScene(scene);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->show();
    view->installEventFilter(this);

    setCentralWidget(view);
}

void TestApp::resizeEvent(QResizeEvent* event)
{
    view->fitInView(0, 0, scene->width(), scene->height());
    QWidget::resizeEvent(event);
}

I’ve tried adding a call to fitInView at the end of TestApp‘s constructor, but it doesn’t seem to do anything – resizeEvent seems to be called once at the start of the program’s execution anyway.

Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:13:29+00:00Added an answer on May 31, 2026 at 10:13 pm

    Handle the view fit also inside the showEvent:

    void TestApp::showEvent ( QShowEvent * event )
    {
        view->fitInView(0, 0, scene->width(), scene->height());
        QWidget::showEvent(event);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.