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 524887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:36:53+00:00 2026-05-13T08:36:53+00:00

This is probably something very obvious, but I have a new to Qt and

  • 0

This is probably something very obvious, but I have a new to Qt and can’t figure it out. I have a simple MainWindow which has one button. When that button is clicked I want to create a QGraphicsScene, add a few lines and then show that in the Window. However when I run this code in a Window it does not show up.

BUT, if I run this as a QApplication, it shows up just fine. What am I missing?

Here is the code in the MainWindow:

void TheDrawings::drawScene() {
 qDebug() << "Setting up Scene";

 QGraphicsScene scene(QRect(-50, -50, 400, 200));

 QPen pen(Qt::red, 3, Qt::DashDotDotLine);

 QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-50, -50, 400,
   200), 0, &scene);
 rectItem->setPen(pen);
 rectItem->setBrush(Qt::gray);

 QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(
   "Amit Bahree", 0, &scene);
 textItem->setPos(50, 0);

 QGraphicsEllipseItem *eclipseItem = new QGraphicsEllipseItem(QRect(170, 20,
   100, 75), 0, &scene);
 eclipseItem->setPen(QPen(Qt::darkBlue));
 eclipseItem->setBrush(Qt::darkBlue);

 QGraphicsPolygonItem *polygonItem = new QGraphicsPolygonItem(QPolygonF(
   QVector<QPointF> () << QPointF(10, 10) << QPointF(0, 90)
     << QPointF(40, 70) << QPointF(80, 110) << QPointF(70, 20)),
   0, &scene);
 polygonItem->setPen(QPen(Qt::darkGreen));
 polygonItem->setBrush(Qt::yellow);

 qDebug() << "Setting up the view";
 QGraphicsView view;
 view.setScene(&scene);
 view.show();

}

  • 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-13T08:36:53+00:00Added an answer on May 13, 2026 at 8:36 am

    your QGraphicsView needs a centralwidget of the mainwindow (or whatever widget you want to put it on top of) to be set as a parent. Also you need to “new” your view and scene objects to put them on the heap so they don’t get destroyed once drawScene finishes. See of following changes to your code would work fine for you:

    QGraphicsScene* scene = new QGraphicsScene(QRect(-50, -50, 400, 200));
    ...
    QGraphicsView* view = new QGraphicsView(ui->centralWidget);
    view->setScene(scene);
    view->setGeometry(QRect(50, 50, 400, 200));
    view->show();
    

    hope this helps, regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a very obvious question but I can't quite figure it out.
This is probably something really simple but for some reason I just can't seem
This has probably something to do with my transformations, but right now I can't
I know this is probably something simple but I can't seem to find anything
This is a very simple (and probably dumb) question, but I can't seem to
I'm a bit new to make, so this is probably something really very simple,
I'm probably missing something very simple but I'm new and it's just easier to
This is probably something very simple but I'm looking for the optimal way of
This is probably something very simple but I'm not getting the results I'm expecting.
This is probably something very simple, so forgive my blonde moment :) I have

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.