[Qt-interest] QAbstractSpinBox question
Kishore Jonnalagadda
kitts.mailinglists at gmail.com
Wed Sep 7 16:12:01 CEST 2011
The important part is the color role that you changed to "Base". You can
apply that on the spinbox itself instead of on the line edit.
--
Cheers!
Kishore
On Sep 7, 2011 7:54 AM, "Doug Stewart" <doug.dastew at gmail.com> wrote:
> Thank you!!!!!! It works!!!
>
> With the following change set all is good :-)
> doug
>
>
> ...**7171*void TextSpinBox::colorChangedSlot(QString
> text)*7272*{*7373* //qDebug() << " Colour was changed.";*74-*
> QPalette p = this->palette();*75-* p.setColor(QPalette::**Text**,
> m_colorMap.value(text));*76-* setPalette(p);*74+* QPalette p =
> this**->lineEdit()**->palette();*75+*
> p.setColor(QPalette::**Base**, m_colorMap.value(text));*76+*
> **this->lineEdit()->**setPalette(p);*7777*}*7878**7979*void
> TextSpinBox::setColourMap()*...
>
>
>
>
>
> **
>
>
> On Tue, Sep 6, 2011 at 9:54 PM, Malyushytsky, Alex <alex at wai.com> wrote:
>
>> 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.”
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>
>
>
>
> --
> DAS
>
> https://linuxcounter.net/user/206392.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110907/d1385621/attachment.html
More information about the Qt-interest-old
mailing list