Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 在QGridLayout中操作QSlider的困难_Qt_Qt4_Qslider - Fatal编程技术网

Qt 在QGridLayout中操作QSlider的困难

Qt 在QGridLayout中操作QSlider的困难,qt,qt4,qslider,Qt,Qt4,Qslider,我试图创建一个动态QSlider,它接受一个整数值的QVector,并将它们映射到QSlider下面,相对于它们所代表的位置 以下是我现在拥有的屏幕截图: 正如您所看到的,刻度与它们的值不一致。这是问题之一 然而,我正在努力解决的主要问题是,无论我将填充值更改为什么(请参见qinteractiveslider.h),我的小部件中的QSlider相对于值网格的大小保持不变 为什么我的QGridLayout的参数不是 提供正确数量的 填充物 我如何强制执行这些页边距 滑翔机 有没有更好的定位方法

我试图创建一个动态QSlider,它接受一个整数值的QVector,并将它们映射到QSlider下面,相对于它们所代表的位置

以下是我现在拥有的屏幕截图:

正如您所看到的,刻度与它们的值不一致。这是问题之一

然而,我正在努力解决的主要问题是,无论我将填充值更改为什么(请参见qinteractiveslider.h),我的小部件中的QSlider相对于值网格的大小保持不变

  • 为什么我的QGridLayout的参数不是 提供正确数量的 填充物
  • 我如何强制执行这些页边距 滑翔机
  • 有没有更好的定位方法 这些标签?我对你敞开心扉 建议
  • 代码如下:

    qinteractiveslider.h

    #ifndef QINTERACTIVESLIDER_H
    #define QINTERACTIVESLIDER_H
    
    #include <QtGui/QGridLayout>
    #include <QtGui/QLabel>
    #include <QtGui/QSlider>
    #include <QtGui/QWidget>
    
    class QInteractiveSlider : public QWidget
     {
         Q_OBJECT
    
    public:
        QInteractiveSlider(QVector<int>* values, int min, int max, QWidget *parent = 0, Qt::WFlags flags = 0);
        ~QInteractiveSlider();
    
    private:
        static const int GRID_WIDTH = 10000;
        static const int PADDING = GRID_WIDTH / 3;
    
        QGridLayout* m_layout;
        QSlider* m_slider;
        QVector<int>* m_values;
    };
    
    \ifndef QINTERACTIVESLIDER\u H
    #定义QINTERACTIVESLIDER_H
    #包括
    #包括
    #包括
    #包括
    类QInteractiveSlider:公共QWidget
    {
    Q_对象
    公众:
    QInteractiveSlider(QVector*值,int-min,int-max,QWidget*父项=0,Qt::WFlags=0);
    ~QInteractiveSlider();
    私人:
    静态常数int GRID_WITH=10000;
    静态常数int PADDING=网格宽度/3;
    QGridLayout*m_布局;
    QSlider*m_滑块;
    QVector*m_值;
    };
    
    qinteractiveslider.cpp

    #include "qinteractiveslider.h"
    
    QInteractiveSlider::QInteractiveSlider(QVector<int>* values, int min, int max, QWidget *parent, Qt::WFlags flags)
        : QWidget(parent, flags)
    {
        m_layout = new QGridLayout();
        m_layout->setSpacing(0);
    
        m_slider = new QSlider(Qt::Horizontal);
        m_slider->setTickInterval(25);
        m_slider->setTickPosition(QSlider::TicksBelow);
    
        m_layout->addWidget(m_slider, 0, PADDING, 1, GRID_WIDTH - PADDING, Qt::AlignBottom);
    
        //populate bottom row with labels
        QVector<int>::Iterator iterator = values->begin();
        while(iterator != values->end()) {
            //place the label at the relative position under the slider
            int columnIndex = ((double)*iterator / (double)max) * (double)GRID_WIDTH;
            QString labelString = QString::number(*iterator);
            QLabel* label = new QLabel(labelString);
            m_layout->addWidget(label, 1, columnIndex, 1, 1, Qt::AlignTop);
    
            iterator++;
        }
    
        this->setLayout(m_layout);
    }
    
    QInteractiveSlider::~QInteractiveSlider()
    {
    
    }
    
    #include "qinteractiveslider.h"
    #include <QtGui/QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QVector<int>* testVector = new QVector<int>();
        testVector->append(0);
        testVector->append(50);
        testVector->append(25);
        testVector->append(100);
    
        QInteractiveSlider* w = new QInteractiveSlider(testVector, 0, 100);
        w->show();
    
        return a.exec();
    }
    
    #包括“qinteractiveslider.h”
    QInteractiveSlider::QInteractiveSlider(QVector*值、int min、int max、QWidget*父项、Qt::WFlags标志)
    :QWidget(父项、标志)
    {
    m_布局=新的QGridLayout();
    m_布局->设置间距(0);
    m_滑块=新的QSlider(Qt::水平);
    m_滑块->设置时间间隔(25);
    m_滑块->设置滴答声位置(QSlider::滴答声如下);
    m_布局->添加小部件(m_滑块,0,填充,1,网格宽度-填充,Qt::AlignBottom);
    //用标签填充底部行
    迭代器迭代器=值->开始();
    while(迭代器!=值->结束(){
    //将标签放置在滑块下的相对位置
    int columnIndex=((双精度)*迭代器/(双精度)最大值)*(双精度)网格宽度;
    QString labelString=QString::number(*迭代器);
    QLabel*标签=新的QLabel(标签字符串);
    m_layout->addWidget(标签,1,columnIndex,1,1,Qt::AlignTop);
    迭代器++;
    }
    此->设置布局(m_布局);
    }
    QInteractiveSlider::~QInteractiveSlider()
    {
    }
    
    main.cpp

    #include "qinteractiveslider.h"
    
    QInteractiveSlider::QInteractiveSlider(QVector<int>* values, int min, int max, QWidget *parent, Qt::WFlags flags)
        : QWidget(parent, flags)
    {
        m_layout = new QGridLayout();
        m_layout->setSpacing(0);
    
        m_slider = new QSlider(Qt::Horizontal);
        m_slider->setTickInterval(25);
        m_slider->setTickPosition(QSlider::TicksBelow);
    
        m_layout->addWidget(m_slider, 0, PADDING, 1, GRID_WIDTH - PADDING, Qt::AlignBottom);
    
        //populate bottom row with labels
        QVector<int>::Iterator iterator = values->begin();
        while(iterator != values->end()) {
            //place the label at the relative position under the slider
            int columnIndex = ((double)*iterator / (double)max) * (double)GRID_WIDTH;
            QString labelString = QString::number(*iterator);
            QLabel* label = new QLabel(labelString);
            m_layout->addWidget(label, 1, columnIndex, 1, 1, Qt::AlignTop);
    
            iterator++;
        }
    
        this->setLayout(m_layout);
    }
    
    QInteractiveSlider::~QInteractiveSlider()
    {
    
    }
    
    #include "qinteractiveslider.h"
    #include <QtGui/QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QVector<int>* testVector = new QVector<int>();
        testVector->append(0);
        testVector->append(50);
        testVector->append(25);
        testVector->append(100);
    
        QInteractiveSlider* w = new QInteractiveSlider(testVector, 0, 100);
        w->show();
    
        return a.exec();
    }
    
    #包括“qinteractiveslider.h”
    #包括
    int main(int argc,char*argv[])
    {
    质量保证申请a(argc、argv);
    QVector*testVector=新的QVector();
    testVector->append(0);
    testVector->append(50);
    testVector->append(25);
    testVector->append(100);
    QInteractiveSlider*w=新的QInteractiveSlider(测试向量,0,100);
    w->show();
    返回a.exec();
    }
    
    我无法直接回答第一个和第二个问题。 单独显示数字是个坏主意。从现在起,您必须知道手动显示数字的位置。这不是很容易移植,并且破坏了封装。它也可能无法正确缩放以适应不断变化的QSlider大小。
    由于QSlider不提供在记号下显示数字的选项,因此您应该扩展QSlider并重写paint()函数,或者使用QWT

    您也可以使用QHBoxLayouts的组合,这样您就可以在没有太多边距问题的情况下正确地分配内容

    下面是以下代码生成的屏幕截图:

    你可以在标签上加一个最大的宽度,使它更漂亮一些。这样可以避免在它们上设置左对齐右对齐

    #ifndef DESIGNERLS7532_H
    #define DESIGNERLS7532_H
    
    #include <QtCore/QVariant>
    #include <QtGui/QAction>
    #include <QtGui/QApplication>
    #include <QtGui/QButtonGroup>
    #include <QtGui/QHBoxLayout>
    #include <QtGui/QHeaderView>
    #include <QtGui/QLabel>
    #include <QtGui/QSlider>
    #include <QtGui/QVBoxLayout>
    #include <QtGui/QWidget>
    
    QT_BEGIN_NAMESPACE
    
    class Ui_Form
    {
    public:
        QVBoxLayout *verticalLayout;
        QWidget *widget;
        QHBoxLayout *horizontalLayout;
        QSlider *horizontalSlider;
        QWidget *widget_2;
        QHBoxLayout *horizontalLayout_2;
        QLabel *label;
        QLabel *label_4;
        QLabel *label_2;
        QLabel *label_5;
        QLabel *label_3;
    
        void setupUi(QWidget *Form)
        {
            if (Form->objectName().isEmpty())
                Form->setObjectName(QString::fromUtf8("Form"));
            Form->resize(268, 55);
            verticalLayout = new QVBoxLayout(Form);
            verticalLayout->setSpacing(0);
            verticalLayout->setContentsMargins(0, 0, 0, 0);
            verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
            widget = new QWidget(Form);
            widget->setObjectName(QString::fromUtf8("widget"));
            horizontalLayout = new QHBoxLayout(widget);
            horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
            horizontalLayout->setContentsMargins(-1, -1, -1, 0);
            horizontalSlider = new QSlider(widget);
            horizontalSlider->setObjectName(QString::fromUtf8("horizontalSlider"));
            horizontalSlider->setOrientation(Qt::Horizontal);
            horizontalSlider->setTickPosition(QSlider::TicksBelow);
    
            horizontalLayout->addWidget(horizontalSlider);
    
    
            verticalLayout->addWidget(widget);
    
            widget_2 = new QWidget(Form);
            widget_2->setObjectName(QString::fromUtf8("widget_2"));
            horizontalLayout_2 = new QHBoxLayout(widget_2);
            horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
            horizontalLayout_2->setContentsMargins(-1, 0, -1, -1);
            label = new QLabel(widget_2);
            label->setObjectName(QString::fromUtf8("label"));
    
            horizontalLayout_2->addWidget(label);
    
            label_4 = new QLabel(widget_2);
            label_4->setObjectName(QString::fromUtf8("label_4"));
            label_4->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
    
            horizontalLayout_2->addWidget(label_4);
    
            label_2 = new QLabel(widget_2);
            label_2->setObjectName(QString::fromUtf8("label_2"));
            label_2->setAlignment(Qt::AlignCenter);
    
            horizontalLayout_2->addWidget(label_2);
    
            label_5 = new QLabel(widget_2);
            label_5->setObjectName(QString::fromUtf8("label_5"));
            label_5->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    
            horizontalLayout_2->addWidget(label_5);
    
            label_3 = new QLabel(widget_2);
            label_3->setObjectName(QString::fromUtf8("label_3"));
            label_3->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    
            horizontalLayout_2->addWidget(label_3);
    
            horizontalLayout_2->setStretch(0, 1);
            horizontalLayout_2->setStretch(1, 1);
            horizontalLayout_2->setStretch(2, 1);
            horizontalLayout_2->setStretch(3, 1);
            horizontalLayout_2->setStretch(4, 1);
    
            verticalLayout->addWidget(widget_2);
    
    
            retranslateUi(Form);
    
            QMetaObject::connectSlotsByName(Form);
        } // setupUi
    
        void retranslateUi(QWidget *Form)
        {
            Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
            label->setText(QApplication::translate("Form", "0", 0, QApplication::UnicodeUTF8));
            label_4->setText(QApplication::translate("Form", "25", 0, QApplication::UnicodeUTF8));
            label_2->setText(QApplication::translate("Form", "50", 0, QApplication::UnicodeUTF8));
            label_5->setText(QString());
            label_3->setText(QApplication::translate("Form", "100", 0, QApplication::UnicodeUTF8));
        } // retranslateUi
    
    };
    
    namespace Ui {
        class Form: public Ui_Form {};
    } // namespace Ui
    
    QT_END_NAMESPACE
    
    #endif // DESIGNERLS7532_H
    
    \ifndef设计人员7532\u H
    #定义DesignerS7532_H
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    QT_开始名称空间
    类别用户界面表格
    {
    公众:
    QVBoxLayout*垂直布局;
    QWidget*widget;
    QHBoxLayout*水平布局;
    QSlider*水平滑块;
    QWidget*widget_2;
    QHBoxLayout*水平布置图2;
    QLabel*标签;
    QLabel*标签4;
    QLabel*标签2;
    QLabel*标签_5;
    QLabel*标签3;
    无效设置UI(QWidget*表单)
    {
    if(Form->objectName().isEmpty())
    表单->setObjectName(QString::fromUtf8(“表单”);
    表单->调整大小(268,55);
    垂直布局=新的QVBoxLayout(表格);
    垂直布局->设置间距(0);
    垂直布局->设置内容边缘(0,0,0,0);
    verticalLayout->setObjectName(QString::fromUtf8(“verticalLayout”);
    widget=新的QWidget(表单);
    widget->setObjectName(QString::fromUtf8(“widget”);
    水平布局=新的QHBoxLayout(小部件);
    horizontalLayout->setObjectName(QString::fromUtf8(“horizontalLayout”);
    水平布局->设置内容边缘(-1,-1,-1,0);
    水平滑块=新的QSlider(小部件);
    horizontalSlider->setObjectName(QString::fromUtf8(“horizontalSlider”);
    水平滑块->设置方向(Qt::水平);
    水平滑块->设置滴答声位置(QSlider::滴答声位于下方);
    水平布局->添加小部件(水平滑块);
    垂直布局->添加小部件(小部件);
    widget_2=新的QWidget(表单);
    widget_2->setObjectName(QString::fromUtf8(“widget_2”);
    horizontalLayout_2=新的QHBoxLayout(小部件_2);
    horizontalLayout_2->setObjectName(QString::fromUtf8(“horizontalLayout_2”);
    水平布局_2->设置内容边缘(-1,0,-1,-1);
    标签=新的QLabel(小部件_2);
    标签->设置对象名(QString::fromUtf8(“标签”);
    水平布局\u 2->addWidget(标签);
    label_4=新的QLabel(小部件_2);
    label_4->setObjectName(QString::fromUtf8(“label_4”);
    label_4->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
    水平布局2->addWidget(标签4);
    label_2=新的QLabel(小部件_2);
    label_2->setObjectName(QString::fromUtf8(“label_2”);
    标签_2->setAlignment(Qt::AlignCenter);