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
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;
};
namespace Ui {
class MainWindow;
}
class MyGraphicsTextItem : public QGraphicsTextItem
{
Q_OBJECT
public:
explicit MyGraphicsTextItem()
{
};
~MyGraphicsTextItem(){};
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *_parent = nullptr) :
QWidget(_parent)
{
parent = _parent;
RenderScene();
};
~MyWidget(){};
QWidget* parent = nullptr;
QGraphicsScene* graphicsScene = nullptr ;
QScrollArea* scrollArea = nullptr;
void RenderScene();
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
MyWidget* myWidget = nullptr;
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
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);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget);
}
void MyWidget::RenderScene()
{
graphicsScene = new QGraphicsScene(this);
MyGraphicsTextItem* graphicsTextItem = new MyGraphicsTextItem();
graphicsScene->addItem(graphicsTextItem);
QGraphicsView* graphicsView = new QGraphicsView(this);
graphicsView->setScene(graphicsScene);
scrollArea = new QScrollArea();
QHBoxLayout* scrollLayout = new QHBoxLayout();
scrollArea->setLayout(scrollLayout);
scrollArea->setWidget(graphicsView);
scrollArea->setWidgetResizable(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MyGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
cout << "Paint called\n";
QRect pixelRect(0, 0, 500, 500);
QPainterPath painterPath;
painterPath.addRect(pixelRect);
painter->setPen(Qt::black);
painter->setRenderHint(QPainter::Antialiasing);
painter->fillPath(painterPath, Qt::white);
QGraphicsTextItem::paint(painter,option,widget);
}
To copy to clipboard, switch view to plain text mode
On the other hand, if I change this
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget);
}
To copy to clipboard, switch view to plain text mode
to this
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget->scrollArea);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myWidget = new MyWidget(this);
this->setCentralWidget(myWidget->scrollArea);
}
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
Bookmarks