在 Qt 中动态创建 QSlider 以及关联的 QLCDNumber

发布于 2024-09-09 23:05:05 字数 220 浏览 1 评论 0原文

我想知道解决以下场景的最佳方法是什么?

我正在动态创建 QSlider,我希望将其链接到关联的 QLCDNumber 进行显示。问题是我希望有十分之一可用,所以我希望在 QSLider 和 QLCDNumber 之间进行转换以除以 10。此时,我真正保留的只是 QSlider,即我刚刚创建并忘记的 QLCDNumber。有没有一种简单的方法可以进行转换和连接,而不必保留太多信息?

提前致谢。

I was wondering what's the best way to go about the following scenario?

I am dynamically creating QSliders that I wish to link to an associated QLCDNumber for display. The thing is I would like to have tenths available, so I would like to have a conversion between the QSLider and the QLCDNumber to divide by 10. At this point all I keep really is the QSlider, the QLCDNumbers I just create and forgot about. Is there an easy way of doing the conversion and connection without having to keep too much information?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-09-16 23:05:05

我会尝试以下方法:

// create a new signal in your parent widget
signals:
  void updateLCDNumber(double);

// and a slot which performs the conversion
private slots:
  void performConversion(int value)
  {
   double convertedValue = value * 0.1;
   emit(updateLCDNumber(convertedValue));
  }

// then set the signals/slots up like this
connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT(performConversion(int)));
connect(this, SIGNAL(updateLCDNumber(double)), myLCDNumber, SLOT(display(double)));

之后您可以完全“忘记”您的液晶显示屏编号,即您不需要保留指针或参考。

编辑:多个滑块的解决方案:

class MySlider : public QSlider
{
 Q_OBJECT
public:
 MySlider(QWidget *parent=0) : QSlider(parent)
 {
   connect(this, SIGNAL(valueChanged(int)), this, SLOT(performConversion(int)));
 }

signals:
  void updateLCDNumber(double);

private slots:
   void performConversion(int value)
   {
     double convertedValue = value * 0.1;
     emit(updateLCDNumber(convertedValue));
   } 
};

现在创建 MySlider 实例而不是 QSlider 实例并连接您的 QLCDNumbers

connect(mySlider1, SIGNAL(updateLCDNumber(double)), myLCDNumber1, SLOT(display(double)));
connect(mySlider2, SIGNAL(updateLCDNumber(double)), myLCDNumber2, SLOT(display(double)));
...

这样您还可以实现不同的转换等因素,只需修改 MySlider 实现即可。

我希望这有帮助。

I'd try something along the following lines:

// create a new signal in your parent widget
signals:
  void updateLCDNumber(double);

// and a slot which performs the conversion
private slots:
  void performConversion(int value)
  {
   double convertedValue = value * 0.1;
   emit(updateLCDNumber(convertedValue));
  }

// then set the signals/slots up like this
connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT(performConversion(int)));
connect(this, SIGNAL(updateLCDNumber(double)), myLCDNumber, SLOT(display(double)));

Afterwards you can completely "forget" about your LCD number, i.e. you don't need to keep a pointer or reference.

EDIT: A solution for several sliders:

class MySlider : public QSlider
{
 Q_OBJECT
public:
 MySlider(QWidget *parent=0) : QSlider(parent)
 {
   connect(this, SIGNAL(valueChanged(int)), this, SLOT(performConversion(int)));
 }

signals:
  void updateLCDNumber(double);

private slots:
   void performConversion(int value)
   {
     double convertedValue = value * 0.1;
     emit(updateLCDNumber(convertedValue));
   } 
};

Now create MySlider instances instead of QSlider ones and connect your QLCDNumbers:

connect(mySlider1, SIGNAL(updateLCDNumber(double)), myLCDNumber1, SLOT(display(double)));
connect(mySlider2, SIGNAL(updateLCDNumber(double)), myLCDNumber2, SLOT(display(double)));
...

This way you can also implement different conversion factors and the like, just modify the MySlider implementation.

I hope that helps.

你的呼吸 2024-09-16 23:05:05

这基本上就是我最终使用的;它似乎有效(尽管它违反了整个面向对象的哲学)。

signalMapper= new QSignalMapper(this);
QObject::connect(tmpSlider, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
sliderMapper->setMapping(tmpSLider, tmpLCDNumber);
QObject::connect(signalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(updateLCD(QWidget*)))

...

void MyClass::updateLCD(QWidget* lcdNum){
    ((QLCDNumber*)lcdNum)->display(((QSlider*)(signalMapper->mapping(lcdNum)))->value()*.1);
}

This is basically what I ended up using; it seems to work (though it violates the whole object oriented philosophy).

signalMapper= new QSignalMapper(this);
QObject::connect(tmpSlider, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
sliderMapper->setMapping(tmpSLider, tmpLCDNumber);
QObject::connect(signalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(updateLCD(QWidget*)))

...

void MyClass::updateLCD(QWidget* lcdNum){
    ((QLCDNumber*)lcdNum)->display(((QSlider*)(signalMapper->mapping(lcdNum)))->value()*.1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文