QT combobox with multiple items at the same line - c++

The buttonbox above has multiple items on the same line. It's a program called draftsight, written in QT.
The buttonbox with the red square is what i have, but i need more info on the same line..
Someone know's how to achieve this??
My current code to add 2 items in the same combobox line :
//setup combobox :
QComboBox *colors = new QComboBox;
ui->toolBar_color->addWidget(colors);
colors->setMinimumWidth(150);
//routine to fill the combobox with external data :
colors->clear();
Dialog_color().extern_toolbar_load();
QPixmap pixmap(15,15);
for(int i = 0; i<red_list.size(); i++){
pixmap.fill(QColor (red_list.at(i),green_list.at(i),blue_list.at(i)));
colors->addItem(pixmap, comments.at(i));
}
So in fact i need something like this :
colors->addItem(pixmap, pixmap, pixmap, "text", "text");
Is there a way?

thanks for your response,
To explain the red_list, green_list, blue list. This are the rgb color value's that user has set up in a tablewidget. The tablewidget data is loaded during program start and during program execution if refreshes from a text file which stores the user defined values. The dialog is not finished, but it's working as expected.
I have been thinking also about combining several pixmap to a long pixmap, but it must be a automated way to combine all rgb value's and other value's like linetype etc which each other. So how can i automate combining several pixmap's to one.. Hmm i have to think about this.
How did they do this in draftsight with QT designer? Default Systems will not provide the solution i think.
All input is welcome !
Edit, i found a solution.. Which can combine things automated when the function example is expanded. So far i am satisfied with the solution.
If qt can do a update for this. That would be nice. For the combobox a pressed signal would be nice also instead of using a event filter..
//setup combobox..
QComboBox *test = new QComboBox;
ui->toolBar_layer->addWidget(test);
test->setMinimumWidth(150);
test->setMinimumHeight(20);
test->addItem("test item");
test->setIconSize(QSize(100, 15));
//setup picture template..
QPixmap pixmap(100, 15);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
//contruct a picture based on rgb colors..
QPixmap pixmap_1(15,15);
pixmap_1.fill(QColor (255,0,0));
painter.drawPixmap(0, 0, 15, 15, pixmap_1);
//load pictures from file..
painter.drawPixmap(30, 0, 15, 15, QPixmap(":/icons/edit-paste.svg"));
painter.drawPixmap(60, 0, 15, 15, QPixmap(":/icons/terminal.svg"));
painter.end();
test->addItem(pixmap, "combobox");

Related

How can i paint something larger on a QListWidgetItem?

I intend to draw a long rectangle bar on a single QListWidgetItem and the code goes like:
QIcon icon(":/black.jpg");
ui->TimeLineDisplayer->addItem("");
ui->TimeLineDisplayer->item(0)->setIcon(icon);
ui->TimeLineDisplayer->setIconSize(QSize(500, 20));
where TimeLineDisplayer is a QListWidget.
The current problem is, there seems to be a limit to the width of the icon. So HOW can i draw a bar long enough on an item of a QListWidget?
my problem is solved by a rather primitive way:
QPixmap icon_pix = QPixmap(":/black.jpg");
ui->TimeLineDisplayer->addItem(0);
ui->TimeLineDisplayer->setIconSize(QSize(2000, 2000));
ui->TimeLineDisplayer->item(0)->setIcon(icon_pix.copy(QRect(0, 0, 500, 20)));
still not sure which sentence makes the real differnce, so do tell me about it if you have any idea.

What might I be doing wrong to draw text with QPainter in QT 5 using C++?

In my paintEvent method for a custom widget for a game I'm writing, After calling the various model objects' render() methods to render them to the widget, I am trying to render the "Hi-Score" text. Here's the general code for just the text drawing:
painter.fillRect(event->rect(), QColor(0, 0, 0));
painter.drawImage(QRectF(event->rect().x(), event->rect().y() + 30, 512, 512), getGameBoardImage());
//...rendering other model components
painter.setBrush(QBrush(QColor(255, 255, 255)));
//painter.setFont(getGameFont());
painter.setFont(QFont("Times New Roman", 16, QFont::Bold));
painter.drawText(0, 0, "HI-SCORE");
I 'was' trying to draw the text in a custom font loaded from resource (I found an answer on here for that) but it wouldn't even display, even with a white brush. I thought maybe it was because it was because I didn't 'set' a font, but setting it to Times New Roman doesn't display anything either. What might I be doing wrong? As you can see the background is a black background with the game board painted on top but leaving a small buffer at the top and bottom. Do I need to do something special to display the text? Please don't suggest using a QLabel because I am trying to keep it all in one widget if possible. If I must, I will split the Hi-Score and 1-Ups into 2 other label sets with specialty fonts.
you code look ok, but you are drawing at 0,0 which is the top left corner of the widget canvas AND the text is actually there but not visible...
draw instead at
painter.drawText(margin, y+margin, "HI-SCORE");
where y is the high of the font used to draw the text and margin is you know a little margin border to make it look better something like 5 units
update
you can get the value of the text you are painting doing somthing like
QFont font("times", 25);
QFontMetrics fm(font);
int pixelsW{fm.width("HI-SCORE")};
int pixelsH{fm.height()};

How to save QPainter instance

I have a Qt application where I use QPainter to draw on a QPixmap which is then through QLabel displayed on the screen. Mainly it's drawing text and it's doing it several times every second.
I see it inefficient when the QPainter instance is constructing several times per second (including it's settings like font, pen etc.). So my question is, how can I save the instance of QPainter and reuse it later? I've tried this but it doesn't preserve the "settings" of the QPainter:
this->painter = new QPainter(&this->canvas); // this->cavas is a QPixmap
painter->setFont(this->font);
painter->setPen(this->font_color);
//...
painter->begin(&this->canvas);
painter->drawText(0, 0, 20, 20, "test");
painter->end();
Thanks

Qt: display an image (QLabel) inside a QScrollArea

I am trying to display an image inside a QScrollArea located on the QMainWindow.
I want a fixed size for the image display, and scroll bars to appear if the loaded image is bigger than the QScrollArea. My problem is that when I load an image which is bigger than the QScrollArea, the image appears cut (which is ok) but no scroll bars appear on the UI.
Taking into account various recommandations from other stackoverflow questions, here is the generated code from the Qt designer:
mImageScrollArea = new QScrollArea(centralWidget);
mImageScrollArea->setObjectName(QString::fromUtf8("mImageScrollArea"));
mImageScrollArea->setGeometry(QRect(440, 0, 400, 700));
mImageScrollArea->setWidgetResizable(false);
scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 398, 698));
mLabel = new QLabel(scrollAreaWidgetContents);
mLabel->setObjectName(QString::fromUtf8("mLabel"));
mLabel->setGeometry(QRect(0, 0, 400, 700));
QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(mLabel->sizePolicy().hasHeightForWidth());
mLabel->setSizePolicy(sizePolicy);
mLabel->setScaledContents(true);
mImageScrollArea->setWidget(scrollAreaWidgetContents);
When an image is loaded, I display it in the label as follow:
QPixmap wPixmap = QPixmap::fromImage(mImage);
ui.mLabel->resize(wPixmap.size());
ui.mLabel->setPixmap(wPixmap);
ui.mLabel->show();
Why aren't any scrollbars showing if the image I load is bigger than the QScrollArea ?
It would be more helpful if you provide UI file content instead of generated C++ code. Anyway, it seems that scrollAreaWidgetContents doesn't have a layout. You need to add a grid layout to it in Qt Designer. After you do this, you will not need to resize label or scrollAreaWidgetContents manually. They will be resized automatically. Calling show on label is not required either, it will be visible by default (unless you have hid it).

QT: picture as window

I want to make window frame using some picture. Window shouldn't have borders, titlebars, etc. It also should be hidden from active windows list (in taskbar).
Second part of question I did with:
this->setAttribute(Qt::WA_NoSystemBackground);
this->setAttribute(Qt::WA_QuitOnClose);
this->setAutoFillBackground(true);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
for new class which inherits QMainWindow. It's hidden, for example, at gnome taskbar, but in Awn (awant windows navigator) I seed it in the list of active windows :(.
What about first part. I did this some time ago with QRegion, QPixmap and mask in overloaded paintEvent. I've lost the code. Can you help me with this?
regarding the first part of the question, you, probably, looking for smth like this:
void MainWindow::paintEvent(QPaintEvent * event)
{
QPainter painter(this);
QPixmap pixmap = QPixmap();
pixmap.load("/home/my_image.jpg");
painter.drawPixmap(event->rect(), pixmap);
}
as an alternative you can create a label and show it on your main window, smth like this:
QLabel* label = new QLabel(this); // where 'this' is your window
label->setGeometry(this->geometry());
QPixmap pixmap = QPixmap();
pixmap.load("/home/my_image.jpg");
label->setPixmap(pixmap.scaled(label->size(), Qt::KeepAspectRatio));
hope this helps, regards