PDA

View Full Version : Drawing shapes on a tabWidget in Qt



fs_tigre
24th April 2012, 17:27
Hi,

I'm currently working on a very basic application that basically grebs the inputs form
two lineEdits displays the result and creates rectanglular shapes when a button is clicked,the
output is based on the two inputs, for instnce if the inputs were 2 and 3 it would draw a 2x3 rectangle (then I duplicate this rectangle based on other factors).

This appliation is a MainWindow using a tabWidget that I created in Qt Creator, all of my buttons and lineEdits are sitting on top of the tabWidget, the problem I'm currently having is that when I draw my rectangles they are actually showing underneath the tabWidget, in other words everything is working except that the rectangular shapes are drawn underneath the tabWidget.

Here is the code that caluculates and draws the recatangles

void MainWindow::calculate()
{
// calculations here
{


//draw rectangles
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter( this);
QRect sheet (20, 20, 120,600);
QBrush brush1(Qt::black, Qt::SolidPattern);
painter.fillRect(sheet, brush1);

if(m_flag)
{
for (int i=0; i< 20; i++)
{
for(int j=0; j<20; j++)
{
QRect rec ((((partWidth+1)*i)+30), (((partLength+1)*j)+30), partWidth, partLength);
QBrush brush(Qt::green, Qt::SolidPattern);
painter.fillRect(rec, brush);
}

}
}
}



// draw rectangles when on_pushButton_Calculate is clicked
void MainWindow::on_pushButton_Calculate_clicked()
{
calculate();
m_flag = true;
update();
}

Is there a way to draw something on a tabWidget?

I know its hard to make a suggestion without actually seeing the whole code but I don't think somebody will want to see
my poor code, I'm actully learning both Qt and C++ at the same time.

Thanks a lot

qlands
25th April 2012, 13:39
Hi,

That happens because you are drawing on the canvas of MainWindow and you should draw in the canvas of the tab page. For this you would need to subclass the tab page and re-implement draw. However, If you want to draw shapes I would encourage you to use QGraphicsView + QGraphicsScene + QGraphicsRectItem.

Carlos.

fs_tigre
25th April 2012, 16:05
Thank you for your reply.

For this you would need to subclass the tab page and re-implement draw.
How would you do this? Sorry about my newbie questions.


However, If you want to draw shapes I would encourage you to use QGraphicsView + QGraphicsScene + QGraphicsRectItem.
If I use the QGraphicsView do I need to subclass the tab page too?

What is the difference between QPainter and QGraphicsView?

Thanks a lot for your help!

qlands
26th April 2012, 15:25
Hi,


If I use the QGraphicsView do I need to subclass the tab page too?

No you don't need to sublclass the tab page because you can place a QGraphicsView in that tab. I am assuming that you are using QTCreator or QT Designer to create your GUIs. Using either of both you can place a QGraphicsView in that page.


What is the difference between QPainter and QGraphicsView?

QGraphicsView is a widget that shows an drawing scene (sometimes called canvas). Each scene can have multiple graphical items (boxes, lines, polygons, etc). The QPainter class performs low-level painting on widgets and other paint devices. If you can to create you a widget from scratch (Analog clock example (http://qt-project.org/doc/qt-4.8/widgets-analogclock.html)) then you need painter. But if you want to draw boxes, lines, polygons, etc use a QGraphicsView + QGraphicsScene.

I recommend you to look at the examples: Diagram Scene (http://qt-project.org/doc/qt-4.8/graphicsview-diagramscene.html) also look at this thread: QGraphicsRectItem-with-list-of-QStrings (http://www.qtcentre.org/threads/48655-QGraphicsRectItem-with-list-of-QStrings)

Carlos

fs_tigre
27th April 2012, 13:43
Thanks a lot for your help. It looks like QPainter is basically to paint actual GUI components. I will try QGraphicsView, I think is what I need.

Thanks a lot