Results 1 to 8 of 8

Thread: Drawing line on table

  1. #1
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Drawing line on table

    Hi folks,
    i just need to draw a line between one cell to an another(they do not have to be in the same row) on a table. I'm using Qt 4.3.4 for windows. I created the table and the cells. I thought that if i can get the coordinate of the cells i can draw the line but i couldn't find the function for it. Can anyone help me?
    Last edited by kocakden; 27th April 2008 at 00:52. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing line on table


  3. #3
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing line on table

    Thanks for quick reply. I read what you sent but i couldn't find out what to do. Can you explain a little bit more?
    Last edited by kocakden; 27th April 2008 at 01:47. Reason: reformatted to look better

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing line on table

    The method returns coordinates of an item you give it as its argument. I think that's what you wanted, right? Then you could do something like:
    Qt Code:
    1. QModelIndex from = ...
    2. QModelIndex to = ...
    3. painter.drawLine(visualRect(from).center(), visualRect(to).center());
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing line on table

    Thanks again but i couldn't do it again. I found a different way to get the coordinates of cells. Here is my codes:

    draw.h:

    Qt Code:
    1. #ifndef DRAW_H
    2. #define DRAW_H
    3.  
    4. #include <QWidget>
    5. #include<QTableWidget>
    6.  
    7. class Draw : public QTableWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. Draw(QWidget *parent = 0);
    12. public slots:
    13. void d_drawline();
    14. };
    15.  
    16.  
    17. #endif
    To copy to clipboard, switch view to plain text mode 

    draw.cpp:
    Qt Code:
    1. #include <QTableWidget>
    2. #include <QTableWidgetItem>
    3. #include <QPainter>
    4. #include <QModelIndex>
    5. #include <QAbstractItemView>
    6.  
    7.  
    8. #include "draw.h"
    9.  
    10. Draw::Draw(QWidget *parent)
    11. {
    12. setRowCount(4);
    13. setColumnCount(4);
    14. this->setGeometry(0, 0, 500, 500);
    15. }
    16.  
    17. void Draw::d_drawline()
    18. {
    19. int x_from = columnViewportPosition(1);
    20. int y_from = rowViewportPosition(1);
    21. int x_to = columnViewportPosition(3);
    22. int y_to = rowViewportPosition(3);
    23.  
    24. QPainter painter(this);
    25. painter.drawLine(x_from,y_from,x_to, y_to);
    26. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QFont>
    3. #include <QGridLayout>
    4. #include <QHBoxLayout>
    5. #include <QPushButton>
    6. #include <QVBoxLayout>
    7.  
    8. #include "draw.h"
    9.  
    10.  
    11. class MyWidget : public QWidget
    12. {
    13. public:
    14. MyWidget(QWidget *parent = 0);
    15. };
    16.  
    17. MyWidget::MyWidget(QWidget *parent)
    18. : QWidget(parent)
    19. {
    20.  
    21. QPushButton *d_drawline = new QPushButton(tr("Draw"));
    22. Draw *draw = new Draw;
    23. connect(d_drawline, SIGNAL(clicked()),draw, SLOT(d_drawline()));
    24.  
    25. QGridLayout *gridLayout = new QGridLayout;
    26. gridLayout->addWidget(d_drawline, 0, 0);
    27. gridLayout->addWidget(draw, 1, 0);
    28. setLayout(gridLayout);
    29.  
    30. }
    31.  
    32. int main(int argc, char *argv[])
    33. {
    34. QApplication app(argc, argv);
    35. MyWidget widget;
    36. widget.setGeometry(200, 200, 500, 355);
    37. widget.show();
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

    it builts successfully but nothing changes when i hit to draw button.
    Last edited by wysota; 28th April 2008 at 16:32. Reason: Please use proper tags - [code] instead of [qtclass]

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing line on table

    But why didn't you use visualRect()? I gave you practically a complete solution...

  7. The following user says thank you to wysota for this useful post:

    kocakden (29th April 2008)

  8. #7
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing line on table

    Because i couldn't understand the QModelIndex class and couldn't use it (. But what is wrong with my code. While i'm debuging the code, i saw that it returns the coordinates correctly.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing line on table

    You can use visualItemRect() if you are using convenience classes. Your code is incorrect because it draws outside paint event.

    Check out this example. Manipulate the spinboxes to see the effect.
    Attached Files Attached Files

  10. The following user says thank you to wysota for this useful post:

    kocakden (29th April 2008)

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.