Results 1 to 7 of 7

Thread: Problems with using multiple instances of the same custom widget.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Problems with using multiple instances of the same custom widget.

    Hi! I'm fairly new to Qt, C++ and OOP all together, but I'm working on a project as a part of a summerjob.

    I'm developing a small custom widget which will graphically display a recived value (something akin to a speedometer), and what I have made so far works to certain degree... untill I try to use more than one instance of the widget in a UI. The first instance displays correctly, but the graphical items in the second one doesn't seem to initialize. On the other hand it seems like the second instance recives input which is actually only connected to the input-slot on the first instance. My gut-feeling says it's something to do with what inherits what?

    All in all, I feel a bit confused. What precautions do you have to take when you're writing a widget meant to be reused several times in a GUI?

    The widget is built so that it can be loaded directly into QtDesigner as a plugin and I use svg-graphics (just mentioning if this could be related to the problem). I've programmed it in QtCreator.

    The widget itself is a huge mess of stuff, so I wrote a smaller example illustrating the basics of how I've made the it and which also demonstrates the same problem. It compiles without any warnings.

    In short: I can't get two instances of a selfmade widget to work in a UI, while one single one works. Example code below:

    displayclass.h
    Qt Code:
    1. #ifndef DISPLAYCLASS_H
    2. #define DISPLAYCLASS_H
    3.  
    4. #include <QtGui/QWidget>
    5. #include <QMainWindow>
    6. #include <QGraphicsView>
    7. #include <QGraphicsScene>
    8. #include <QGraphicsSvgItem>
    9. #include <QGraphicsItem>
    10. #include <QSvgRenderer>
    11. #include <QTimer>
    12. #include <QDebug>
    13. //#include <qdesignerexportwidget.h>
    14. #include <QLabel>
    15.  
    16. class /*QDESIGNER_WIDGET_EXPORT*/ displayclass : public QWidget
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. displayclass(QWidget *parent = 0);
    22.  
    23. QSvgRenderer *svgRend;
    24. QLabel *label;
    25. QGraphicsRectItem *rectangle;
    26. QTimer *timer;
    27. void UpdateAll(double rotation, int save_angle);
    28. void DisplaySquare(double rotation);
    29. void DisplayArrow();
    30. void DisplayLabel(double rotation);
    31.  
    32. public slots:
    33. void Input(int rotation);
    34.  
    35. private slots:
    36. void Timer();
    37. };
    38.  
    39. #endif
    To copy to clipboard, switch view to plain text mode 

    displayclass.cpp
    Qt Code:
    1. #include "displayclass.h"
    2.  
    3. displayclass::displayclass(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. //Definitions
    7. scene = new QGraphicsScene(QRect(0, 0, 300, 300),this);
    8. svgRend = new QSvgRenderer(QLatin1String(":/arrow.svg"),this);
    9. view = new QGraphicsView(this);
    10. arrow = new QGraphicsSvgItem();
    11. rectangle = new QGraphicsRectItem(0,0,100,100);
    12. label = new QLabel(this);
    13.  
    14. timer = new QTimer(this);
    15. connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
    16. timer->start(30);
    17.  
    18. scene->setSceneRect(0,0,300,300);
    19. view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    20. view->setGeometry(QRect(0, 0, 350, 350));
    21. view->setScene(scene);
    22. view->show();
    23. UpdateAll(0,0);
    24. }
    25.  
    26. void displayclass::UpdateAll(double rotation, int save_angle)
    27. {
    28. static double rot=0;
    29.  
    30. if(save_angle==1)
    31. {
    32. rot=rotation;
    33. }
    34. else
    35. {
    36. DisplaySquare(rot);
    37. DisplayArrow();
    38. DisplayLabel(rot);
    39. }
    40. }
    41.  
    42. void displayclass::DisplaySquare(double rotation)
    43. {
    44. static int rectangle_activated=0;
    45.  
    46. if(rectangle_activated==0)
    47. {
    48. rectangle->setBrush( Qt::red );
    49. rectangle->setPos(0,0);
    50. qDebug()<<rectangle->pos();
    51. rectangle_activated=1;
    52. scene->addItem(rectangle);
    53. }
    54. rectangle->setRotation(rotation);
    55. }
    56.  
    57. void displayclass::DisplayArrow()
    58. {
    59. static int arrow_activated=0;
    60.  
    61. if(arrow_activated==0)
    62. {
    63. arrow->setParentItem(rectangle);
    64. arrow->setSharedRenderer(svgRend);
    65. arrow->setPos(0,0);
    66. }
    67. }
    68.  
    69. void displayclass::DisplayLabel(double rotation)
    70. {
    71. static bool label_activated=0;
    72.  
    73. if(label_activated==0)
    74. {
    75. label->setParent(this);
    76. label->setGeometry((scene->width()/2)-25,(scene->height()/2)-(label->height()/2),50,25);
    77. label->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
    78. label->setAutoFillBackground(1);
    79. label->setStyleSheet("QLabel { border-width: 2px; border-radius: 4px; border-style: solid; border-color: darkGray; background-color: white;}");
    80. label_activated=1;
    81. }
    82. label->setText(QString::number(rotation));
    83. }
    84.  
    85. void displayclass::Timer()
    86. {
    87. UpdateAll(0,0);
    88. }
    89.  
    90. void displayclass::Input(int rotation)
    91. {
    92.  
    93. UpdateAll(rotation,1);
    94.  
    95. }
    To copy to clipboard, switch view to plain text mode 

    I also attached a picture of how it looks when I use two instances in a quickly put together application.
    Both of the instances are declared like so in ui_mainwindow of the application:
    Qt Code:
    1. displayclass1 = new displayclass(centralWidget);
    2. displayclass1->setObjectName(QString::fromUtf8("displayclass1"));
    3. displayclass1->setGeometry(QRect(40, 60, 211, 231));
    4. displayclass2 = new displayclass(centralWidget);
    5. displayclass2->setObjectName(QString::fromUtf8("displayclass2"));
    6. displayclass2->setGeometry(QRect(350, 60, 201, 231));
    To copy to clipboard, switch view to plain text mode 
    Hmm, anything else I should specify if it could be helpful?

    Thanks for any help in advance! (oh and sorry for any bad English!)
    Any comments about obvious c++/Qt basics I've missed are also welcome!
    Attached Images Attached Images

Similar Threads

  1. Replies: 1
    Last Post: 30th December 2008, 13:28
  2. Replies: 1
    Last Post: 1st February 2008, 18:55
  3. Problems with laying out custom widget
    By mtrpoland in forum Newbie
    Replies: 6
    Last Post: 19th August 2007, 11:43
  4. Multiple program instances
    By scwizard in forum Qt Programming
    Replies: 13
    Last Post: 1st April 2007, 17:42
  5. Replies: 0
    Last Post: 21st December 2006, 11:48

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
  •  
Qt is a trademark of The Qt Company.