如何获取光标当前位置的编号(qt)

时间:2010-07-15 15:01:22

标签: qt qt-creator

我需要小帮助,我正在使用qt的Qslider类,我怎样才能获得光标当前位置的编号(我可以做哪个功能),提前感谢

编辑

我想实现一件事:当我达到我想要退出的间隔的最大值时,我怎么能用插槽和信号做到这一点?

1 个答案:

答案 0 :(得分:2)

我假设您想要滑块的值?

int QSlider::value ()

我看了你的另一篇文章中的代码,这是我在清理后想出来的:

<强>的main.cpp

#include <QApplication>
#include "mywidget.h"

int main(int argc, char *argv[])
{
 QApplication app(argc, argv);
 MyWidget widget;
 widget.show();
 return app.exec();
}

<强> mywidget.h

 #ifndef MYWIDGET_H
 #define MYWIDGET_H
 #include <QWidget>
 #include <QObject>
 include <QPushButton>
 #include <QSlider>

 class MyWidget : public QWidget
 {
  Q_OBJECT
  public:
MyWidget(QWidget *parent = 0);
~MyWidget(){};
  public slots:
void getSliderValueAndQuit();
  private:
 QPushButton *quit;
 QSlider *slider;
 };

 #endif // MYWIDGET_H

<强> myWidget.cpp

 #include "mywidget.h"
 #include <QWidget>
 #include <QObject>
 #include <QApplication>
 #include <QFont>
 #include <QLCDNumber>
 #include <QPushButton>
 #include <QSlider>
 #include <QVBoxLayout>

 MyWidget::MyWidget(QWidget *parent) :
 QWidget(parent)
 {
quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));

QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);

slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

connect(slider, SIGNAL(valueChanged(int)),lcd, SLOT(display(int)));
connect(slider,SIGNAL(sliderReleased()), SLOT(getSliderValueAndQuit()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
 }

 void MyWidget::getSliderValueAndQuit(){
if(slider->value() == slider->maximum())
    close();
 }