PDA

View Full Version : QSpinBox



ToddAtWSU
17th June 2008, 17:11
I have some QSpinBoxes in which I always want to display 2 digits, even if the value is a single digit value. In other words, for values like 1 and 5 I would like to display 01 and 05. I have looked into valueFromText and textFromValue but don't think I need these since I am still just displaying integers and not characters besides 0-9. I also looked at validate, but I can't think of why I would need validate because the QSpinBox already limits the user to entering digits. I just want to prefix the single digit values with a 0. I thought about connecting the valueChanged signal to a slot to add a prefix to the number if it were less than 10, but would prefer to not handle this by connecting to the signal in case I need to capture this signal at a later time and do more complex things with it. Thanks for any ideas you may have!

jpn
17th June 2008, 17:23
I have looked into valueFromText and textFromValue but don't think I need these since I am still just displaying integers and not characters besides 0-9.
Yes, this is the way to go.

ToddAtWSU
17th June 2008, 18:03
How exactly do I use textFromValue and valueFromText? I was hoping to avoid them because the docs don't really explain this very well. What exactly am I supposed to do with these functions and how I do get the QSpinBox to display my 2 digit numbers? Are there any examples of this? Thanks again!

jpn
17th June 2008, 18:19
In a locale unaware way it's as simple as this:


class SpinBox : public QSpinBox
{
protected:
virtual QString textFromValue(int value) const
{
return QString("%1").arg(value, 2, 10, QLatin1Char('0'));
}
};

Andrewgaven
27th August 2012, 05:42
Hello, Sorry for reviving an old post.
Did this solution work for you?, I need to implement two or more digits on a Spinbox, but I don't know how, I used Designer so I don't have the Spinbox class, is there any other way?
Thanks in advance

Ginsengelf
28th August 2012, 08:04
Hi, in the Qt Designer you can use "Promote to" to create a SpinBox instead of QSpinBox. Just create a QSpinBox, right-click and select "Promote to", then in the dialog enter the details of your class, in this case SpinBox.

Ginsengelf

Andrewgaven
30th August 2012, 03:18
Thanks for the reply, I Have been trying for a couple of days, with no succest :( I created a QWidget with a QSpinbox and a label, then promote the QSpinbox to spinbox.h, this is my spinbox.h file


#ifndef SPINBOX_H
#define SPINBOX_H

#include <QSpinBox>
#include <QString>

class SpinBox : public QSpinBox
{
protected:
virtual QString textFromValue(int value) const
{
return QString("%1").arg(value, 2, 10, QLatin1Char('0'));
}
};

#endif // SPINBOX_H


this code produce a error


ui_myqwidget.h:35: error: no matching function for call to 'SpinBox::SpinBox(QWidget*&)'


Wich is generated by designer, I'm I doing something wrong?

Ginsengelf
30th August 2012, 08:04
Hi, your promotion dialog should look like this:
8155
Then click "Add" and "Promote", and Designer should use your SpinBox instead of the QSpinBox. Make sure the path for the header file is correct.

Ginsengelf

Andrewgaven
31st August 2012, 04:51
I don't think the problem is on Designer, I'm runing QT on Linux (that why it looks a little bit different)
8162
and this are the issues that the compiler give me
8163

my Qwidget is named pcargar.ui
as you can see the error point to autogenerated ui_pcargar.h line 35


/************************************************** ******************************
** Form generated from reading UI file 'pcargar.ui'
**
** Created: Thu Aug 30 17:42:07 2012
** by: Qt User Interface Compiler version 4.7.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
************************************************** ******************************/

#ifndef UI_PCARGAR_H
#define UI_PCARGAR_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QWidget>
#include "spinbox.h"

QT_BEGIN_NAMESPACE

class Ui_pcargar
{
public:
SpinBox *spinBox;
QLabel *label;

void setupUi(QWidget *pcargar)
{
if (pcargar->objectName().isEmpty())
pcargar->setObjectName(QString::fromUtf8("pcargar"));
pcargar->resize(400, 300);
spinBox = new SpinBox(pcargar);
spinBox->setObjectName(QString::fromUtf8("spinBox"));
spinBox->setGeometry(QRect(190, 50, 56, 22));
label = new QLabel(pcargar);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(260, 60, 57, 14));

retranslateUi(pcargar);

QMetaObject::connectSlotsByName(pcargar);
} // setupUi

void retranslateUi(QWidget *pcargar)
{
pcargar->setWindowTitle(QApplication::translate("pcargar", "Form", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("pcargar", "TextLabel", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class pcargar: public Ui_pcargar {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_PCARGAR_H

Any ideas.

d_stranz
31st August 2012, 18:50
Promoting the widget in Designer has nothing to do with this error. Your definition of your SpinBox class is incorrect. Read what the compiler is telling you: It is looking for a constructor SpinBox( QWidget *& ). First, you need to define (and implement it). Second, QSpinBox is a QObject-based class, so any class derived from it must also include the Q_OBJECT macro as part of the class definition so the Qt meta-object and signal/sot mechanism work correctly for your class.



class SpinBox : public QSpinBox
{
Q_OBJECT

public:
SpinBox( QWidget * parent ) : QSpinBox( parent ) {}

protected:
virtual QString textFromValue(int value) const
{
return QString("%1").arg(value, 2, 10, QLatin1Char('0'));
}
};

Andrewgaven
1st September 2012, 04:36
Sorry for the newbie mistake that solve the problem, Thank you very much.