Hello,

I am trying to add a feature to a lab I am working on where I have to have a way to show images of the 20 pen widths the Easy Paint Lab can do. I am trying to use a ComboBox from Qt in order to do that. This is what I have so far, I cannot see the ComboBox anywhere in my program.

#include "toolbar.h"
#include "colorchooser.h"
#include "../datasingleton.h"

#include <QtGui/QToolButton>
#include <QtGui/QGridLayout>
#include <QtGui/QSpinBox>
#include <QtGui/QAction>
#include <QtCore/QMap>
#include <QComboBox>
#include <QIcon>


ToolBar::ToolBar(const QMap<InstrumentsEnum, QAction *> &actMap, QWidget *parent) :
    QToolBar(tr("Instruments"), parent), mActMap(actMap)
{
    setMovable(false);
    initializeItems();
    mPrevInstrumentSetted = false;
}

QToolButton* ToolBar::createToolButton(QAction *act)
{
    QToolButton *toolButton = new QToolButton();
    toolButton->setMinimumSize(QSize(30, 30));
    toolButton->setMaximumSize(QSize(30, 30));
    toolButton->setDefaultAction(act);
    toolButton->setStatusTip(act->text());
    return toolButton;
}

void ToolBar::initializeItems()
{
    mCursorButton = createToolButton(mActMap[CURSOR]);
    mEraserButton = createToolButton(mActMap[ERASER]);
    mPenButton = createToolButton(mActMap[PEN]);
    mLineButton = createToolButton(mActMap[LINE]);
    mColorPickerButton = createToolButton(mActMap[COLORPICKER]);
    mMagnifierButton = createToolButton(mActMap[MAGNIFIER]);
    mSprayButton = createToolButton(mActMap[SPRAY]);
    mFillButton = createToolButton(mActMap[FILL]);
    mRectangleButton = createToolButton(mActMap[RECTANGLE]);
    mEllipseButton = createToolButton(mActMap[ELLIPSE]);
    mCurveButton = createToolButton(mActMap[CURVELINE]);
    mTextButton = createToolButton(mActMap[TEXT]);
    mRRectangleButton = createToolButton(mActMap[RECTANGLE2]);


    QGridLayout *bLayout = new QGridLayout();
    bLayout->setMargin(3);
    bLayout->addWidget(mCursorButton, 0, 0);
    bLayout->addWidget(mEraserButton, 0, 1);
    bLayout->addWidget(mColorPickerButton, 1, 0);
    bLayout->addWidget(mMagnifierButton, 1, 1);
    bLayout->addWidget(mPenButton, 2, 0);
    bLayout->addWidget(mLineButton, 2, 1);
    bLayout->addWidget(mSprayButton, 3, 0);
    bLayout->addWidget(mFillButton, 3, 1);
    bLayout->addWidget(mRectangleButton, 4, 0);
    bLayout->addWidget(mEllipseButton, 4, 1);
    bLayout->addWidget(mCurveButton, 5, 0);
    bLayout->addWidget(mTextButton, 5, 1);
    bLayout->addWidget(mRRectangleButton, 6, 0);

    QWidget *bWidget = new QWidget();
    bWidget->setLayout(bLayout);

    mPColorChooser = new ColorChooser(0, 0, 0, this);
    mPColorChooser->setStatusTip(tr("Primary color"));
    mPColorChooser->setToolTip(tr("Primary color"));
    connect(mPColorChooser, SIGNAL(sendColor(QColor)), this, SLOT(primaryColorChanged(QColor)));

    mSColorChooser = new ColorChooser(255, 255, 255, this);
    mSColorChooser->setStatusTip(tr("Secondary color"));
    mSColorChooser->setToolTip(tr("Secondary color"));
    connect(mSColorChooser, SIGNAL(sendColor(QColor)), this, SLOT(secondaryColorChanged(QColor)));

    QSpinBox *penSizeSpin = new QSpinBox();
    penSizeSpin->setRange(1, 20);
    penSizeSpin->setValue(1);
    penSizeSpin->setStatusTip(tr("Pen size"));
    penSizeSpin->setToolTip(tr("Pen size"));
    connect(penSizeSpin, SIGNAL(valueChanged(int)), this, SLOT(penValueChanged(int)));

    //Here is my code for the images to appear in the ComboBox
    QIcon PenWidth1(":/media/instruments-icons/PenWidth1.png");
    QIcon PenWidth2(":/media/instruments-icons/PendWidth2.png");
    QIcon PenWidth3(":/media/instruments-icons/PendWidth3.png");
    QIcon PenWidth4(":/media/instruments-icons/PendWidth4.png");
    QIcon PenWidth5(":/media/instruments-icons/PendWidth5.png");
    QIcon PenWidth6(":/media/instruments-icons/PendWidth6.png");
    QIcon PenWidth7(":/media/instruments-icons/PendWidth7.png");
    QIcon PenWidth8(":/media/instruments-icons/PendWidth8.png");
    QIcon PenWidth9(":/media/instruments-icons/PendWidth9.png");
    QIcon PenWidth10(":/media/instruments-icons/PendWidth10.png");
    QIcon PenWidth11(":/media/instruments-icons/PendWidth11.png");
    QIcon PenWidth12(":/media/instruments-icons/PendWidth12.png");
    QIcon PenWidth13(":/media/instruments-icons/PendWidth13.png");
    QIcon PenWidth14(":/media/instruments-icons/PendWidth14.png");
    QIcon PenWidth15(":/media/instruments-icons/PendWidth15.png");
    QIcon PenWidth16(":/media/instruments-icons/PendWidth16.png");
    QIcon PenWidth17(":/media/instruments-icons/PendWidth17.png");
    QIcon PenWidth18(":/media/instruments-icons/PendWidth18.png");
    QIcon PenWidth19(":/media/instruments-icons/PendWidth19.png");
    QIcon PenWidth20(":/media/instruments-icons/PendWidth20.png");

    //Here I am trying to get my ComboBox to appear and connect the first image
    //to the ComboBox
    QWidget w;

    QVBoxLayout *layout = new QVBoxLayout(&w);
    QLabel *label = new QLabel("Pen Width", &w);
    QComboBox *MyPenWidth= new QComboBox(&w);
    layout->addWidget(label);
    layout->addWidget(MyPenWidth);

    MyPenWidth->setItemIcon(1, PenWidth1);
    //connect(MyPenWidth, SIGNAL(PenWidth1), SLOT(PenWidth1));
    connect(MyPenWidth, SIGNAL(ToolBar(PenWidth1)), label, SLOT(ToolBar(PenWidth1)));



    QGridLayout *tLayout = new QGridLayout();
    tLayout->setMargin(3);
    tLayout->addWidget(mPColorChooser, 0, 0);
    tLayout->addWidget(mSColorChooser, 0, 1);
    tLayout->addWidget(penSizeSpin, 1, 0, 1, 2);

    QWidget *tWidget = new QWidget();
    tWidget->setLayout(tLayout);

    addWidget(bWidget);
    addSeparator();
    addWidget(tWidget);
}

void ToolBar::penValueChanged(const int &value)
{
    DataSingleton::Instance()->setPenSize(value);
}

void ToolBar::primaryColorChanged(const QColor &color)
{
    DataSingleton::Instance()->setPrimaryColor(color);
}

void ToolBar::secondaryColorChanged(const QColor &color)
{
    DataSingleton::Instance()->setSecondaryColor(color);
}

void ToolBar::setPrimaryColorView()
{
    mPColorChooser->setColor(DataSingleton::Instance()->getPrimaryColor());
}

void ToolBar::setSecondaryColorView()
{
    mSColorChooser->setColor(DataSingleton::Instance()->getSecondaryColor());
}

void ToolBar::contextMenuEvent(QContextMenuEvent *)
{
}

You have made your QWidget w a proper local variable of ToolBar::initializeItems(). There is nothing bad about that, but it means, that your widget will get deleted, when ToolBar::initializeItems() ends, taking along all of its children.
You would want to make QWidget w an instance variable of your ToolBar or make it a child of your toolbar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.