[Qt-interest] QAbstractSpinBox question
Malyushytsky, Alex
alex at wai.com
Wed Sep 7 03:54:46 CEST 2011
Spin box is complex control, which uses QLineEdit.
So I guess you need to set background color for
QAbstractSpinBox::lineEdit()
To get the pointer to line edit correctly you need to subclass QAbstractSpinBox.
But you don't have to if you use QObject functionality, something like:
QAbstractSpinBox* spinBox =...; // your spinbox
QLineEdit * edit = spinBox ->findChild< QLineEdit *>( QLatin1String("qt_spinbox_lineedit"));
Regards,
Alex
From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of Doug Stewart
Sent: Tuesday, September 06, 2011 6:17 PM
To: Qt Interest
Subject: [Qt-interest] QAbstractSpinBox question
I am new to qt and C++ === so any suggestions will be appreciated.
I am trying to make a text spinbox from QAbstractSpinBox such that the color of each item
in the spinbox is different. So far I have the forground text changing color like I want it by using
QPalette::Text
I then tried to change the background color by using
QPalette::Window but this does not work.
Is there a way to change the background color in my textspinbox?
Doug
This is textspinbox.cpp
#include "textspinbox.h"
#include <QAbstractSpinBox>
#include <QDebug>
#include <QPalette>
TextSpinBox::TextSpinBox(QWidget* parent)
: QAbstractSpinBox(parent), m_list(0), m_index(0), m_colorMap()
{
this->setWrapping(true);
this->stepEnabled();
this->setMinimumSize(150, 30);
connect(this, SIGNAL(colorChanged(QString)),
this, SLOT(colorChangedSlot(QString)));
}
TextSpinBox::~TextSpinBox()
{
/* Set the pointer to null, and then the stack part of it will be
* handled regularly.*/
m_list = 0;
}
void TextSpinBox::setText(const QString& text)
{
if(m_list != 0)
{
m_index = m_list->indexOf(text);
this->lineEdit()->setText(m_list->at(m_index));
setColourMap();
emit colorChanged(m_list->at(m_index));
}
else
qDebug() << "The TextSpinBog QStringList is not initialized properly.";
}
/* This is the best way, and most secure way of setting up the spin box. */
void TextSpinBox::init(const QStringList &list, const QString &text)
{
m_list = &list;
m_index = m_list->indexOf(text);
this->lineEdit()->setText(m_list->at(m_index));
setColourMap();
emit colorChanged(m_list->at(m_index));
}
void TextSpinBox::stepBy(int steps)
{
m_index += steps;
if( m_index >= m_list->size() )
m_index = 0;
else if( m_index < 0 )
m_index = m_list->size()-1;
this->lineEdit()->setText( m_list->at(m_index) );
emit colorChanged(m_list->at(m_index));
emit textChanged();
}
QAbstractSpinBox::StepEnabled TextSpinBox::stepEnabled() const
{
return QAbstractSpinBox::StepDownEnabled|QAbstractSpinBox::StepUpEnabled;
}
void TextSpinBox::colorChangedSlot(QString text)
{
//qDebug() << " Colour was changed.";
QPalette p = this->palette();
p.setColor(QPalette::Text, m_colorMap.value(text));
setPalette(p);
}
void TextSpinBox::setColourMap()
{
int size = 128 / m_list->size();
for (int i = m_list->size() -1 ; i >= 0 ; i--)
{
/*These colours are hard coded to try to match the present
octave colour settings in a different part of the program.
*/
QColor color( 30, 127 + i*size, 127 - i*size);
m_colorMap.insert(m_list->at(i), color);
}
}
--
DAS
https://linuxcounter.net/user/206392.html
---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.
“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”
“Please consider our environment before printing this email.”
More information about the Qt-interest-old
mailing list