Results 1 to 4 of 4

Thread: QGraphicsView get fullscreen

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default QGraphicsView get fullscreen

    Hi all,

    I'm tring to develop a calendar like those of Outlook, or Google Calendar. To do that I'm using a QGraphicsView, QGraphicsScene and QGraphicsItem(s). At the moment I draw a grid that represents hours, so there is a solid line for each hour and a dotline for each half hour. I developed it with a small monitor, and when I render it with a bigger one my QGraphicsView is bigger than my scene. So there is white space on both left and right side of my graph. I'm going to attach a picture to show the defect. I tried to use this code to fix:

    Qt Code:
    1. void GraphicsView::populateScene()
    2. {
    3. m_scene = new QGraphicsScene(this);
    4. setScene(m_scene);
    5. qDebug() << "scene rect: " << m_scene->sceneRect();//this prints: "scene rect: QRectF(0,0 0x0)"
    6. fitInView(m_scene->sceneRect());
    7. qDebug() << "scene rect: " << m_scene->sceneRect();//this prints: "scene rect: QRectF(0,0 0x0)"
    8. drawGrid();
    9. this->fitInView(m_scene->sceneRect());
    10. qDebug() << "scene rect: " << m_scene->sceneRect();//and finally this works: "scene rect: QRectF(-0.5,-0.5 1001x1153) "
    11. //Use ScrollHand Drag Mode to enable Panning
    12. setDragMode(ScrollHandDrag);
    13. }
    To copy to clipboard, switch view to plain text mode 

    so as far as I can understand only last fitInView call works, but after that call the view is empty, while if I remove that call I get what you can see in attached picture.

    This is the code I used to obtain that grid:

    Qt Code:
    1. void GraphicsView::drawGrid()
    2. {
    3. const int linesNumber = 48;
    4. int counter=0;
    5. qreal leftLine=0.0;
    6. qreal y = 0;
    7. qreal gridSize = m_fontFactor * 2;
    8. QTime time;
    9. QFont textFont("Times New Roman", m_fontFactor);
    10. QPen redPen(QColor(255, 0, 0, 127),1.0,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
    11. QPen halfHourPen(QColor(201,201,201, 255),1.0,Qt::DotLine,Qt::RoundCap,Qt::RoundJoin);
    12. QPen hourPen(QColor(201, 201, 201, 255),1.0,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
    13. m_eventRect = QRectF();
    14. for(int i=counter;i<linesNumber;i++)
    15. {
    16. if(i%2==0)
    17. {//draw hour line:
    18. time.setHMS(counter++,0,0);
    19. item = m_scene->addText(time.toString("hh:mm"),textFont);
    20. item->setPos(10, y);//set up text's position
    21. if(leftLine == 0.0)
    22. leftLine = item->boundingRect().width();
    23. m_scene->addLine(0,y,1000,y,hourPen);
    24. m_lines[i] = QRectF(leftLine+11,y+1,1000-5,gridSize-2);
    25. }else{//draw half hour line:
    26. m_scene->addLine(leftLine+10,y,1000,y,halfHourPen);
    27. m_lines[i] = QRectF(leftLine+11,y+1,1000-5,gridSize-2);
    28. }
    29. y += gridSize;
    30. }
    31. m_scene->addLine(leftLine+10,0,leftLine+10,y,redPen);
    32. m_eventRect.setX(0.0);
    33. m_eventRect.setY(0.0);
    34. m_eventRect.setWidth(1000.0);
    35. m_eventRect.setHeight(y);
    36.  
    37. slotUpdateHourHighlightLine();
    38. setSceneRect(0, 0, 1000, y);
    39. }
    To copy to clipboard, switch view to plain text mode 

    m_lines is a QVector<QRectF> used to store where coords' line, so I can use them later in my code (to attach an event with a fixed hour/half hour position)
    How can I get size to avoid using "magic numbers" into my code? I mean how to get a width to always has a fullscreen graph? and how to draw always a fullscreen graph?
    Thanks for any reply.
    Alberto
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsView get fullscreen

    How can I get size to avoid using "magic numbers" into my code? I mean how to get a width to always has a fullscreen graph? and how to draw always a full-screen graph?
    1. Educate the QGraphicsScene and the line items in it about the QGraphicsView size (essentially width), and re-size the items when ever the QGraphcisView is re-sizes.
    2. Don't use QGraphicsScene / QGraphicsView, instead try to achieve it by using Qt layout managers and Qt Widgets to get such view
    3. Use QWidget's painting functions to draw the lines and text on the widget as required (instead of the scene)


    I see the 3rd option as the best way out in this particular case of calendar application / widget.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGraphicsView get fullscreen

    Thanks for your reply.
    I'm going to try your last advice.

    Alberto

  4. #4
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGraphicsView get fullscreen

    Hi again,

    I'm not going to open a new thread as my question is the natural continuous of this. I hope this is fine.
    I tried to drow my calendar in a QWidget as Santosh suggested. But some stuff are hard to do.

    When I zoom in scrollbar can't let me see the whole calendar. Maybe it's a QScrollArea issue.

    I read this post for widgets nesting. Adding a further QWidget I got QScrollBars visible. So actually my widgets nesting is this:

    Qt Code:
    1. QGroupBox (with vertical layout)
    2. |
    3. |-some stuff for header bar (with zoom in/out buttons, and so on)
    4. |-QScrollArea (with vertical layout, widgetResizable property set to "true")
    5. |
    6. |-QWidget (as used into above post)
    7. |
    8. |-QWidget promoted to my DailyWidget class
    To copy to clipboard, switch view to plain text mode 

    I has a part of the code into an Abstract class as I plan to create several calendar's views (daily,weekly,and so on..)
    Therefore zoom in/out methods are into this abstract class:

    Qt Code:
    1. //zoom in and out:
    2. void AbstractWidget::slotZoom(int delta)
    3. {
    4. qreal scaleTemp = m_scaleFactor;
    5. if(delta>0)
    6. {
    7. scaleTemp *= 1.25;
    8. }else{
    9. scaleTemp *= 0.8;
    10. }
    11. if(scaleTemp>=1.0 && scaleTemp<3.0)
    12. {
    13. m_scaleFactor = scaleTemp;
    14.  
    15. if(!m_scrollarea)
    16. getScrollArea();
    17. if(m_scrollarea)
    18. {
    19. QScrollBar *sb = m_scrollarea->horizontalScrollBar();
    20.  
    21. adjustScrollBar(sb, scaleTemp);
    22.  
    23. sb = m_scrollarea->verticalScrollBar();
    24.  
    25. adjustScrollBar(sb, scaleTemp);
    26. }
    27. update();
    28. updateGeometry();
    29. }
    30. }
    31.  
    32. void AbstractWidget::adjustScrollBar(QScrollBar *scrollBar, double factor)
    33. {
    34. if(scrollBar)
    35. {
    36. scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    getScrollArea method is a bit ugly and it gets the scroll area rely on the widgets nesting to get scrollarea:

    Qt Code:
    1. void AbstractWidget::getScrollArea()
    2. {
    3. m_scrollarea = (QScrollArea *)this->parent()->parent()->parent()->parent();
    4. }
    To copy to clipboard, switch view to plain text mode 

    and finally this is the painting code of my DailyWidget:

    Qt Code:
    1. DailyWidget::DailyWidget(QWidget *parent)
    2. : AbstractWidget(parent),
    3. m_timer(new QTimer(this))
    4. {
    5. m_view = AbstractWidget::DailyView;
    6. m_scaleFactor = 1.0;
    7. m_fontFactor = 12;
    8. setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    9. connect(m_timer, SIGNAL(timeout()), this, SLOT(slotUpdateHourHighlightLine()));
    10. }
    11.  
    12. void DailyWidget::paintEvent(QPaintEvent *event)
    13. {
    14. QWidget::paintEvent(event);
    15. QPainter m_painter(this);
    16. m_painter.scale(m_scaleFactor,m_scaleFactor);
    17. m_painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing|QPainter::HighQualityAntialiasing,true);
    18. QSize s(997*m_scaleFactor,580*m_scaleFactor);
    19. resize(s);
    20. drawGrid(m_painter);
    21. }
    22.  
    23. void DailyWidget::drawGrid(QPainter &painter)
    24. {
    25. const int linesNumber = 48;
    26. QSize msize = size();
    27. int width = msize.width();
    28. int counter = 0;
    29. qreal leftLine = 0.0;
    30. qreal y = 10;//initialized with paddingtop
    31. qreal gridSize = msize.height()/(qreal)linesNumber;
    32. QTime time;
    33. QFont textFont("Times New Roman",gridSize/2);
    34. painter.setBrush(whiteBrush);
    35. painter.setPen(blackPen);
    36. painter.drawRect(0,0,width,msize.height());
    37.  
    38. painter.setFont(textFont);
    39. m_eventRect = QRectF();
    40. painter.setBrush(whiteBrush);
    41. time.setHMS(counter+1,0,0);
    42. QRectF textRect = painter.boundingRect(QRect(0,0,0,0),time.toString("hh:mm"));
    43. leftLine = textRect.width()+10;
    44. y=0;
    45. painter.setBrush(whiteBrush);
    46. for(int i=counter;i<linesNumber;i++)
    47. {
    48. if(i%2==0)
    49. {//hours:
    50. painter.setPen(hourPen);
    51. painter.drawLine(0,y,width,y);
    52.  
    53. time.setHMS(counter++,0,0);
    54. painter.setPen(blackPen);
    55. painter.drawText(5,y+textRect.height(),time.toString("hh:mm"));
    56. }else{//half hour:
    57. painter.setPen(halfHourPen);
    58. painter.drawLine(leftLine+10,y,width,y);
    59. }
    60. y += gridSize;
    61. }
    62. painter.setPen(hourPen);
    63. painter.drawLine(0,y,width,y);
    64.  
    65. painter.setPen(redPen);
    66. painter.drawLine(leftLine+10,0,leftLine+10,y);
    67. m_eventRect.setX(0.0);
    68. m_eventRect.setY(0.0);
    69. m_eventRect.setWidth(width);
    70. m_eventRect.setHeight(y);
    71. QSize AdjustSize = this->size();
    72. setMinimumSize(AdjustSize);
    73. }
    To copy to clipboard, switch view to plain text mode 

    As explained above when I zoom in drawing is zoomed as well, but QScrollArea can't let user reach the end of chart.
    I'm going to attach two images, respectively zoom out and zoom in.

    Thanks all for any advice.
    Alberto


    zoomout.jpgzoomin.jpg

Similar Threads

  1. cannot exit from fullscreen
    By lazycoder in forum Qt Programming
    Replies: 1
    Last Post: 7th October 2011, 06:38
  2. Fullscreen in qml
    By vinayaka in forum Newbie
    Replies: 2
    Last Post: 3rd October 2011, 07:05
  3. QGraphicsItem to fullscreen
    By medved6 in forum Qt Programming
    Replies: 2
    Last Post: 16th June 2010, 22:54
  4. QMenu bar in FullScreen
    By ppaluch in forum Newbie
    Replies: 0
    Last Post: 14th August 2009, 15:52
  5. Fullscreen
    By dragor in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2006, 21:23

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.