Results 1 to 2 of 2

Thread: Subclassing from QGraphicsTextItem and QScrollArea issue

  1. #1
    Join Date
    Jul 2011
    Location
    California
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Subclassing from QGraphicsTextItem and QScrollArea issue

    Hello Everyone

    I want to create a main window with a scrollable QGraphicsScene which has objects subclassed from QGraphicsTextItem. Basically, I want to draw a white rectangle with text inside it (the code to draw text inside the rectangle is not shown), and when the rectangle is bigger than the viewport, I want scroll bars to automatically appear in the QGraphicsScene

    In the code snippets below I have simplified the code with just one object of MyGraphicsTextItem in the QGraphicsScene object. All I see with the code below is a blank screen, the overridden MyGraphicsTextItem:: paint() does not get called.

    mainwindow.h
    Qt Code:
    1. namespace Ui {
    2. class MainWindow;
    3. }
    4.  
    5. class MyGraphicsTextItem : public QGraphicsTextItem
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit MyGraphicsTextItem()
    10. {
    11.  
    12. };
    13. ~MyGraphicsTextItem(){};
    14.  
    15. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    16. };
    17.  
    18. class MyWidget : public QWidget
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. explicit MyWidget(QWidget *_parent = nullptr) :
    24. QWidget(_parent)
    25. {
    26. parent = _parent;
    27.  
    28. RenderScene();
    29.  
    30.  
    31. };
    32.  
    33. ~MyWidget(){};
    34.  
    35. QWidget* parent = nullptr;
    36. QGraphicsScene* graphicsScene = nullptr ;
    37. QScrollArea* scrollArea = nullptr;
    38. void RenderScene();
    39.  
    40. };
    41.  
    42. class MainWindow : public QMainWindow
    43. {
    44. Q_OBJECT
    45.  
    46. public:
    47. explicit MainWindow(QWidget *parent = nullptr);
    48. ~MainWindow();
    49.  
    50. private:
    51. Ui::MainWindow *ui;
    52.  
    53. MyWidget* myWidget = nullptr;
    54. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. myWidget = new MyWidget(this);
    8.  
    9. this->setCentralWidget(myWidget);
    10.  
    11. }
    12.  
    13.  
    14. void MyWidget::RenderScene()
    15. {
    16. graphicsScene = new QGraphicsScene(this);
    17.  
    18. MyGraphicsTextItem* graphicsTextItem = new MyGraphicsTextItem();
    19. graphicsScene->addItem(graphicsTextItem);
    20.  
    21. QGraphicsView* graphicsView = new QGraphicsView(this);
    22. graphicsView->setScene(graphicsScene);
    23.  
    24. scrollArea = new QScrollArea();
    25.  
    26. QHBoxLayout* scrollLayout = new QHBoxLayout();
    27. scrollArea->setLayout(scrollLayout);
    28.  
    29. scrollArea->setWidget(graphicsView);
    30. scrollArea->setWidgetResizable(true);
    31.  
    32.  
    33. }
    34.  
    35. MainWindow::~MainWindow()
    36. {
    37. delete ui;
    38. }
    39.  
    40. void MyGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    41. {
    42. cout << "Paint called\n";
    43. QRect pixelRect(0, 0, 500, 500);
    44.  
    45. QPainterPath painterPath;
    46. painterPath.addRect(pixelRect);
    47.  
    48. painter->setPen(Qt::black);
    49. painter->setRenderHint(QPainter::Antialiasing);
    50.  
    51. painter->fillPath(painterPath, Qt::white);
    52.  
    53. QGraphicsTextItem::paint(painter,option,widget);
    54. }
    To copy to clipboard, switch view to plain text mode 

    On the other hand, if I change this

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. myWidget = new MyWidget(this);
    8.  
    9. this->setCentralWidget(myWidget);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    to this

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. myWidget = new MyWidget(this);
    8.  
    9. this->setCentralWidget(myWidget->scrollArea);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    I can see the white rectangle, however I don't get scrollbars in the window.
    Screen Shot 2019-01-06 at 5.10.59 PM.jpg
    Last edited by sreenidhi707; 7th January 2019 at 02:30. Reason: updated contents

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,867
    Thanks
    223
    Thanked 601 Times in 591 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Subclassing from QGraphicsTextItem and QScrollArea issue

    First, you have your logic turned around backwards. The scroll area should be the central widget for the MainWindow, you should put a layout inside that, and then you should add your "MyWidget" instance into the layout. My widget should not be managing the scroll area, it should be the other way around.

    Second, the items in QGraphicsScene instances are rendered into QGraphicsView instances. There is really no need for you to have a "MyWidget" class, simply put the QGraphicsView as the widget managed by the layout in the scroll area.

    Finally, there will be no scrollbars if the scene is smaller than the view. You might be drawing into a QRect that is 500 x 500, but nowhere do you tell the scene that your custom text object is that big.

    There are many examples of the Graphics / View architecture in the Qt distribution, including some that do the same things you are trying to do.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. issue resizing QScrollArea pyqt5
    By Bybuu in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2017, 10:22
  2. Replies: 14
    Last Post: 18th June 2011, 11:32
  3. QScrollArea and Painting issue
    By myfifth in forum Newbie
    Replies: 4
    Last Post: 21st January 2011, 09:21
  4. QGraphicsTextItem setPlainText Memory issue
    By decin in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2010, 04:03
  5. Subclassing QGraphicsTextItem and QGraphicsLayoutItem
    By psih128 in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2008, 21:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.