1 Attachment(s)
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
Code:
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MyGraphicsTextItem()
{
};
~MyGraphicsTextItem(){};
};
{
Q_OBJECT
public:
explicit MyWidget
(QWidget *_parent
= nullptr
) : {
parent = _parent;
RenderScene();
};
~MyWidget(){};
void RenderScene();
};
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= nullptr
);
~MainWindow();
private:
Ui::MainWindow *ui;
MyWidget* myWidget = nullptr;
};
mainwindow.cpp
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget);
}
void MyWidget::RenderScene()
{
MyGraphicsTextItem* graphicsTextItem = new MyGraphicsTextItem();
graphicsScene->addItem(graphicsTextItem);
graphicsView->setScene(graphicsScene);
scrollArea->setLayout(scrollLayout);
scrollArea->setWidget(graphicsView);
scrollArea->setWidgetResizable(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
cout << "Paint called\n";
QRect pixelRect
(0,
0,
500,
500);
painterPath.addRect(pixelRect);
painter->setPen(Qt::black);
painter
->setRenderHint
(QPainter::Antialiasing);
painter->fillPath(painterPath, Qt::white);
}
On the other hand, if I change this
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget);
}
to this
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget->scrollArea);
}
I can see the white rectangle, however I don't get scrollbars in the window.
Attachment 13006
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.