KM
Oct. 7, 2020, 11:50 a.m.

Как тут сделать мой виджет другим цветом медленно и красиво(возможн при помощи дюрейшн, или любым другим способом)

qt, setDuration, Animation, Widget, ui, label

  1. #include "pinpoint.h"
  2. #include "ui_pinpoint.h"
  3. #include <QPropertyAnimation>
  4. #include <interfaces/fastproxystyle.h>
  5.  
  6. Connection& PinPoint::getConnection()
  7.  
  8. {
  9. return connection;
  10. }
  11.  
  12. int PinPoint::getId() const
  13. {
  14. return id;
  15. }
  16.  
  17. void PinPoint::setId(int value)
  18. {
  19. id = value;
  20. }
  21.  
  22. PinPoint::PinPoint(QWidget *parent) :
  23. QWidget(parent),
  24. connection(nullptr),
  25. ui(new Ui::PinPoint),
  26. notConnected("Punkt", "Blad", Error::Priority::CriticalError, true),
  27.  
  28. m_colorAnimation(this, "color"),
  29.  
  30. zoneViolation("Alarm", "Naruszenie strefy", Error::Priority::CriticalError, true,true,true,"Sprawdź stan strefy")
  31. {
  32. ui->setupUi(this);
  33. this->layout()->setAlignment(ui->label, Qt::AlignCenter);
  34. connect(ui->pin, &PointShape::mouseOver, this, &PinPoint::mouseOverPin);
  35. connect(ui->pin, &PointShape::setupDeclared, this, &PinPoint::setupDeclared);
  36. connect(&connection, &Connection::currentStateUpdated, this, &PinPoint::updateState);
  37. connect(&connection, &Connection::updateView, this, &PinPoint::updateView);
  38. connect(&connection.getZonesWidget(), &ZonesListWidget::updateDB, this, &PinPoint::onUpdateEmit);
  39. this->setStyle(new FastProxyStyle(this->style()));
  40. connect(ui->pin, &PointShape::mouseOver1, this, &PinPoint::NoWidget);
  41. // connect(this, &PinPoint::mouseOverPin, this, [ui->pin](){ ui->pin->setStyleSheet("background-color: red;"); });
  42. // m_colorAnimation(this, "color");
  43.  
  44.  
  45. //connect(ui->pin, &PointShape::mouseOver1, this, &PinPoint::mouseOverPin2);
  46.  
  47. }
  48.  
  49. void PinPoint::fillSetup(const QString& address, const QString& name) {
  50. setIP(address);
  51. setName(name);
  52. connection.setConnectionData(getIP(), 7094);
  53. }
  54.  
  55. void PinPoint::updateState(CustomState *state, QList<Zone*> source){
  56. if(!source.isEmpty()) {
  57. if(state==&connection.getZonesWidget().alarm || state==&connection.getZonesWidget().silentAlarm) {
  58. QString helpString="";
  59. foreach(auto item, source) {
  60. helpString+=item->getId()+" ";
  61. }
  62. zoneViolation.setSource(this->getText());
  63. zoneViolation.setValue("Problem w jednostce "+this->getText()+" (IP: "+this->getIP().toString()+"). Wystąpił "+state->getName()+" w strefach: "+helpString);
  64. generateError(zoneViolation);
  65. }
  66. else cancelError(zoneViolation);
  67. }
  68.  
  69. ui->pin->setState(*state);
  70. refreshHint();
  71. //TUTAJ DODAC ALARMY
  72. /*if(ui->pin->state!=_state && (_state == Global::States::ALARM || _state==Global::States::CON_LOST))
  73. generateError(notConnected);
  74. else if((ui->pin->state==Global::States::ALARM && _state!=Global::States::ALARM) || (ui->pin->state==Global::States::CON_LOST && _state!=Global::States::CON_LOST))
  75. cancelError(notConnected);
  76.  
  77. ui->pin->setState(_state);*/
  78. }
  79.  
  80. void PinPoint::updateView(const QHostAddress& address, uint16_t port)
  81. {
  82. ui->label_2->setText(address.toString());
  83. Q_UNUSED(port)
  84. }
  85.  
  86. void PinPoint::onUpdateEmit()
  87. {
  88. emit(updateDatabase(*this));
  89. }
  90.  
  91. void PinPoint::copy()
  92.  
  93. {
  94.  
  95.  
  96. }
  97.  
  98. void PinPoint::setPos(const QPointF &pos) {
  99. position = pos;
  100. this->move(Converter::toPointToMiddle(position, size()));
  101. }
  102.  
  103. bool PinPoint::isFakeElement()
  104. {
  105. return (getIP().toString()=="0.0.0.0");
  106.  
  107. }
  108.  
  109.  
  110. void PinPoint::SetColor(const QColor& color)
  111. {
  112. m_currentColor = color;
  113. // QString css = "QPushButton { border-radius: 5px; ";
  114. // css.append("border: 1.5px solid rgb(91,231,255); ");
  115. QString strColor = QString("rgb(%1, %2, %3)").arg(color.red()).arg(color.green()).arg(color.blue());
  116. // css.append("background-color: " + strColor + "; }");
  117. // setStyleSheet(css);
  118. }
  119.  
  120.  
  121.  
  122. const QColor& PinPoint::GetColor() const
  123. {
  124. return m_currentColor;
  125. }
  126.  
  127.  
  128.  
  129. void PinPoint::mouseOverPin(){
  130. raise();
  131. if(!isFakeElement())
  132.  
  133. emit mouseOverMe();
  134.  
  135. m_colorAnimation.stop();
  136.  
  137. m_colorAnimation.setDuration(900);
  138. m_colorAnimation.setStartValue(GetColor());
  139. m_colorAnimation.setEndValue(QColor(100, 100, 100));
  140.  
  141. m_colorAnimation.setEasingCurve(QEasingCurve::Linear);
  142.  
  143. m_colorAnimation.start();
  144.  
  145.  
  146. // ui->label->setStyleSheet("font-size: 13px;\ncolor: black;\nbackground-color: limegreen ;\npadding: 1px 3px 2px 3px;\nborder-top: 1px solid rgb(20, 19, 50);\nborder-radius: 5px;\nfont-weight:400; ");
  147. // ui->widget->setStyleSheet("border-top-left-radius: 3px;\nborder-top-right-radius: 3px;\nborder-bottom-right-radius: 3px;\nborder-bottom-left-radius: 3px; background-color: black ; ");
  148. // ui->label_2->setStyleSheet("color:white;\nfont-size: 11px;\nfont-weight:500;\nbackground:darkgreen;\npadding:1px 3px; ");
  149.  
  150. }
  151.  
  152. void PinPoint::NoWidget()
  153. {
  154.  
  155.  
  156. m_colorAnimation.stop();
  157.  
  158. m_colorAnimation.setDuration(900);
  159. m_colorAnimation.setStartValue(GetColor());
  160. m_colorAnimation.setEndValue(QColor(255, 0, 0));
  161.  
  162. m_colorAnimation.setEasingCurve(QEasingCurve::Linear);
  163.  
  164. m_colorAnimation.start();
  165.  
  166. //StartHoverEnterAnimation();
  167. // m_colorAnimation.stop();
  168.  
  169. // m_colorAnimation.setDuration(900); //set your transition
  170. // m_colorAnimation.setStartValue(GetColor()); //starts from current color
  171. // m_colorAnimation.setEndValue(QColor(255, 0, 0));//set your regular color
  172.  
  173. // m_colorAnimation.setEasingCurve(QEasingCurve::Linear);//animation style
  174.  
  175. // m_colorAnimation.start();
  176.  
  177. // m_colorAnimation.stop();
  178.  
  179. // m_colorAnimation.setDuration(900); //set your transition
  180. // m_colorAnimation.setStartValue(Qt::White); //starts from current color
  181. // m_colorAnimation.setEndValue(QColor(100, 100, 100));//set your hover color
  182.  
  183. // m_colorAnimation.setEasingCurve(QEasingCurve::Linear);//animation style
  184.  
  185. // m_colorAnimation.start();
  186.  
  187. // ui->label->setStyleSheet("font-size: 13px;\ncolor: rgb(225, 225, 245);\nbackground-color: rgb(31, 34, 68);\npadding: 1px 3px 2px 3px;\nborder-top: 1px solid rgb(20, 19, 50);\nborder-radius: 5px;\nfont-weight:400; ");
  188. // ui->widget->setStyleSheet("border-top-left-radius: 3px;\nborder-top-right-radius: 3px;\nborder-bottom-right-radius: 3px;\nborder-bottom-left-radius: 3px; ");
  189. // ui->label_2->setStyleSheet("color:white;\nfont-size: 11px;\nfont-weight:500;\nbackground:rgb(34, 34, 34);\npadding:1px 3px; ");
  190.  
  191. }
  192.  
  193. void PinPoint::setName(const QString &text)
  194. {
  195. ui->label->setText(text);
  196. notConnected.setSource(text);
  197. notConnected.setValue("Alarm w obiekcie "+text+" (ip:"+getIP().toString()+")!");
  198. }
  199. void PinPoint::refreshHint() {
  200. this->setToolTip("Nazwa: "+ui->label->text()+"\nIP: "+this->getIP().toString()+"\n"+connection.getZonesWidget().getInfoString());
  201. }
  202. void PinPoint::setIP(const QString& ip)
  203. {
  204. connection.setCentralAddress(QHostAddress(ip));
  205. ui->label_2->setText(ip);
  206. refreshHint();
  207. }
  208.  
  209. PinPoint::~PinPoint(){delete ui;}
  210. QPointF PinPoint::getPos() {return position;}
  211. QString PinPoint::getText() {return this->ui->label->text();}
  212. QString PinPoint::getStateString(){return ui->pin->getStateString();}
  213. QHostAddress PinPoint::getIP(){return connection.getCentralAddress();}
1

Do you like it? Share on social networks!

0

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup