Bonjour,
Dans une application que je développe, je dois faire défiler un curseur sur des boutons. La solution choisie est de colorer le cadre du bouton lorsque le curseur passe sur ce bouton. Cependant l'application plante lorsque le thread modifie la couleur du cadre et cela seulement sous windows, sous Linux cela fonctionne.
Le code du Curseur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 #include "Cursor.h" Cursor* Cursor::instance = NULL; // Method to get (and if necessary create) the instance of Cursor Cursor* Cursor::getInstance() { if(Cursor::instance == NULL) Cursor::instance = new Cursor(); return Cursor::instance; } /***************************************************************************/ // Method to destroy the instance void Cursor::killInstance() { if(Cursor::instance != NULL) delete Cursor::instance; } Cursor :: Cursor () : currentElement(NULL) { } /** * * if you use this methode when the cursor is scrolling, the cursor stop */ void Cursor::setVectorToScroll (vector<Element*>* vectElem) { this->vectorToScroll = vectElem; this->index = 0; this->currentElement = NULL; this->started = false; } /** * @param interval time in milisec */ void Cursor :: setInterval(int interval) { this->interval = interval; } int Cursor :: getInterval() { return interval; } bool Cursor :: isStarted () { return started; } /** * if the cursor not scrolling, this method return NULL */ Element* Cursor :: getCurrentElement () { if (this->started) return this->currentElement; else return NULL; } void Cursor :: stop () { if (started == true){ started = false; //join if (currentElement != NULL) currentElement->highlight(false); } } void Cursor::run() { this->started = true; while(started) { if(currentElement != NULL) currentElement->highlight(false); this->currentElement = vectorToScroll->at(index%vectorToScroll->size()); this->currentElement->highlight(true); this->interval = str2int(XML::Parser::getInstance()->getConfiguration("cursor-delay")); this->msleep(interval); index++; if( index >= this->vectorToScroll->size()) index = 0; } } void Cursor::select() { this->currentElement->reEmitClicked(); }D'élément :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 #ifndef CURSOR_H #define CURSOR_H #include <QThread> #include <string> #include "Button.h" #include "utils.h" #include "Log.h" using namespace std; class Cursor : public QThread { public : static Cursor* getInstance(); static void killInstance(); bool isStarted (); void setVectorToScroll (vector<Element*>*); void setInterval(int ); int getInterval (); //void start(); void run(); void stop(); Element* getCurrentElement (); void select(); protected: Cursor (); private : static Cursor* instance; // Glib::Thread* m_thread; Element* currentElement; //sigc::connection conn; vector<Element*>* vectorToScroll; int index; int interval; bool started; // sigc::signal<void> test; }; #endif
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 #include "Element.h" #include "Log.h" Element::Element() : QPushButton(), img(NULL) { this->iconButton = new QPushButton(); layout = new QVBoxLayout; layout->addWidget(this->iconButton,0,Qt::AlignHCenter); this->setLayout(layout); QObject::connect(this->iconButton, SIGNAL(clicked()), this, SLOT(reEmitClicked())); this->selected = false; this->highlighted = false; //this->setColor(this, "#ffffff"); //this->setColor(this->main, "green"); } Element::Element(const Element &anElement) : QPushButton() { this->img = new QIcon(*anElement.img); this->iconButton = new QPushButton(anElement.iconButton); this->iconAdress=anElement.iconAdress; this->selected=anElement.selected; } void Element::operator=(const Element &anElement){ this->img = new QIcon(*anElement.img); this->iconButton = new QPushButton(anElement.iconButton); this->iconAdress=anElement.iconAdress; this->selected=anElement.selected; } Element::~Element() { delete this->iconButton; if (this->img != NULL) delete this->img; } // @param size is the size of the width and height because the images are square void Element::resizeImg(unsigned int size){ if (this->img!=NULL && size>0) { QSize theSize(size,size); this->iconButton->setIconSize(theSize); } } void Element::select(bool s) { /* if(s && !this->selected) this->setBackgroundColor(this, "red"); else if(!s && this->selected) this->setBackgroundColor(this, "blue"); this->selected = s; */ } void Element::highlight(bool h) { if(h && !this->highlighted) this->setBorderColor(this->iconButton, "green"); else if(!h && this->highlighted) this->setBorderColor(this->iconButton, "blue"); this->highlighted = h; } bool Element::isSelected() { return this->selected; } void Element::setBorderColor(QWidget *part, string color) { QString style = "border: 3px solid "; style += color.c_str(); part->setStyleSheet(style); } void Element::setBackgroundColor(QWidget *part, string color) { QString style = "background-color: "; style += color.c_str(); part->setStyleSheet(style); } void Element::reEmitClicked() { emit clicked(); }Merci s'il vous plaît d'éclairer ma lanterne sur ce problème car il m'handicape vraiment.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 #ifndef ELEMENT #define ELEMENT #define BORDER 25 #include <string> //#include <gtkmm.h> //#include <gtkmm/button.h> #include <QPushButton> #include <QVBoxLayout> using namespace std; class Element : public QPushButton { Q_OBJECT public: Element(); ~Element(); Element(const Element &Element); virtual void operator=(const Element &Element); void highlight(bool); void select(bool); bool isSelected(); void resizeImg(unsigned int size); public slots: void reEmitClicked(); protected: QIcon *img; QSize *imgSize; QVBoxLayout *layout; void setBackgroundColor(QWidget*, string); void setBorderColor(QWidget*, string); QPushButton *iconButton; string iconAdress; bool selected; bool highlighted; }; #endif
Partager