Advertisement
PavloPonomarov

SignalTest

Jan 9th, 2020
2,929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 2.88 KB | None | 0 0
  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. import QtQuick.Controls 2.12
  4. import com.test.generator 1.0
  5.  
  6. Window {
  7.     visible: true
  8.     width: 640
  9.     height: 480
  10.     title: qsTr("Hello World")
  11.  
  12.     Generator{
  13.         id: generator
  14.     }
  15.  
  16.     FlickableTextEdit{
  17.         id: edit
  18.         x: 5
  19.         y: 5
  20.         width: 50
  21.         height: 20
  22.     }
  23.  
  24.     ComboBox{
  25.         x: 80
  26.         y: 5
  27.         id: combo
  28.         property var obj
  29.         model: ListModel {
  30.                 id: model
  31.                 ListElement { text: "Parameter1" }
  32.                 ListElement { text: "Parameter2" }
  33.         }
  34.         onActivated: {
  35.             if(obj) obj.destroy();
  36.             let pref;
  37.             if(currentIndex === 0){
  38.                 pref = "1"
  39.             }
  40.             else{
  41.                 pref = "2"
  42.             }
  43.             obj = Qt.createQmlObject("import QtQuick 2.12; Connections{ target: generator; onParameter"+pref+"Changed: edit.text = value; }", edit, "Connection");
  44.         }
  45.     }
  46. }
  47.  
  48. //generator.h
  49. #ifndef GENERATOR_H
  50. #define GENERATOR_H
  51.  
  52. #include <QObject>
  53. #include <QTimer>
  54.  
  55. class Generator : public QObject
  56. {
  57.     Q_OBJECT
  58.     Q_PROPERTY(double parameter1 READ parameter1 WRITE setParameter1 NOTIFY parameter1Changed)
  59.     Q_PROPERTY(double parameter2 READ parameter2 WRITE setParameter2 NOTIFY parameter2Changed)
  60. public:
  61.     explicit Generator(QObject *parent = nullptr);
  62.  
  63.     double parameter1() const;
  64.     void setParameter1(double parameter1);
  65.  
  66.     double parameter2() const;
  67.     void setParameter2(double parameter2);
  68.  
  69. private:
  70.     double m_parameter1;
  71.     double m_parameter2;
  72.  
  73.     QTimer timer1;
  74.     QTimer timer2;
  75.  
  76. signals:
  77.     void parameter1Changed(double value);
  78.     void parameter2Changed(double value);
  79.  
  80. public slots:
  81.     void increase();
  82.     void decrease();
  83.  
  84. };
  85.  
  86. #endif // GENERATOR_H
  87.  
  88. //generator.cpp
  89. #include "generator.h"
  90.  
  91. Generator::Generator(QObject *parent) : QObject(parent)
  92. {
  93.     m_parameter1 = 0;
  94.     m_parameter2 = 100000;
  95.     connect(&timer1, SIGNAL(timeout()), this, SLOT(increase()));
  96.     connect(&timer2, SIGNAL(timeout()), this, SLOT(decrease()));
  97.     timer1.start(1000);
  98.     timer2.start(1000);
  99. }
  100.  
  101. double Generator::parameter1() const
  102. {
  103.     return m_parameter1;
  104. }
  105.  
  106. void Generator::setParameter1(double ascending)
  107. {
  108.     m_parameter1 = ascending;
  109.     emit parameter1Changed(m_parameter1);
  110. }
  111.  
  112. double Generator::parameter2() const
  113. {
  114.     return m_parameter2;
  115. }
  116.  
  117. void Generator::setParameter2(double descending)
  118. {
  119.     m_parameter2 = descending;
  120.     emit parameter2Changed(m_parameter2);
  121. }
  122.  
  123. void Generator::increase()
  124. {
  125.     m_parameter1++;
  126.     if(m_parameter1 > 100000) m_parameter1 = 0;
  127.     emit parameter1Changed(m_parameter1);
  128. }
  129.  
  130. void Generator::decrease()
  131. {
  132.     m_parameter2--;
  133.     if(m_parameter2 < 0) m_parameter2 = 100000;
  134.     emit parameter2Changed(m_parameter2);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement