Display QImage within main window - c++

I'm trying to display an image with Qt, I can get it to appear in a separate window, but I can't make it appear within the main window
Qt_first w;
w.show();
This shows the window I designed in Qt designer, how do I access the Qlabel(Image_Lbel) I put within the QWidget (centralWidget) of that window?
I generated a stripy image that shows correctly, just not within the correct window
QSize size = QSize(640,480);
QImage::Format format = QImage::Format_ARGB32;
QImage image = QImage::QImage(size, format);
//generate
QLabel myLabel;
myLabel.setPixmap(QPixmap::fromImage(image));
myLabel.show();
I get the feeling it could be I haven't included the files from the creator or the namespaces any suggestion much appreciated

I guess your Label is getting displayed independently. Set the parent of label to your main window. Then your Label will displayed inside your main window.
So use,
QLabel *myLabel = new QLabel(this); // sets parent of label to main window
myLabel->setPixmap(QPixmap::fromImage(image));
myLabel->show();
You can also use move function for moving your label within the main window.

If you want to set the label from outside the Qt_first class, you need to add a method to do this. For example (in qt_first.cpp, change qt_first.h accordingly):
void Qt_first::setImageLabel(const QImage& image)
{
ui->Image_Lble.setPixmap(QPixmap::fromImage(image));
}
ui in this example is the object that represents the UI that you created with Qt Designer.

I used a combination of both answers, thanks to both
Qt_first w; // the UI I made with Qt creator
QLabel *myLabel = w.getImageLabel();
myLabel->setPixmap(QPixmap::fromImage(image));
w.show();
With the following inside the Qt_first class
QLabel* Qt_first::getImageLabel(){
QLabel *myLabel = ui.Image_Label;
return myLabel;
}

Related

Moving QMainWindow does not affect where the child widgets are being drawn

I'm pretty new to the Qt environment. I have an application that I want to display on a second monitor. Currently, in the MainWindow constructor, I'm moving the window to a second monitor using the following code.
main():
MainWindow w;
w.showFullScreen();
MainWindow() constructor:
QRect screenres = QApplication::desktop()->screenGeometry(1);
this->move(QPoint(screenres.x(), screenres.y()));
this->resize(screenres.width(), screenres.height());
This works for my main window. The problem is that all the children widgets still get displayed on the first monitor. I have a menu widget that is part of the centralWidgetFrame that gets created in the constructor after the move(), but it doesn't get created on the second monitor. From my understanding, the child widgets should be created in positions relative to their parent.
The MainWindow returns a pos() of (1920,0) as expected and the child menu widget gives me a pos() of (0,0).
I'm using Qt 4.7.1. Any suggestions?
I think this is because you're moving and resizing the MainWindow in its constructor.
Do it in main():
MainWindow w;
QRect screenres = QApplication::desktop()->screenGeometry(1);
w.move(QPoint(screenres.x(), screenres.y()));
w.resize(screenres.width(), screenres.height());
w.showFullScreen();

How do I just put header and some content under it in Qt Designer?

QtCreator's designer allows you to edit user interface graphically. I was just trying to make some sense of it - what I wanted was a header text centered in the middle and some widget under it, like this:
But my results look like this, when using vertical layout:
I placed QLabel on top and QOpenGLWidget on bottom - I only used QOpenGlWidget because it has black background on screenshot. What I really plan on doing is using another QWidget. I used vertical layout then. So how do I get the result on the first image, using QLabel and QWidget?
Qt's use of XML is not a replacement for HTML.
HTML is designed for marking up web pages. Qt's widgets are not web pages!
It appears that you're looking at the box with the text 'content' and thinking of a generic widget. I see that and see a QLabel, which is derived from QWidget after all.
It's probably easier to explain in code how I would go about this, which you can then translate to doing the same in Qt Creator.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a root widget (this could also be a QMainWindow, or any other widget)
QWidget* pWidget = new QWidget;
// Layout to arrange the widgets vertically
QVBoxLayout* pBoxLayout = new QVBoxLayout;
pWidget->setLayout(pBoxLayout);
// The header widget
QLabel* pHeader = new QLabel("Header");
pHeader->setAlignment(Qt::AlignCenter);
pHeader->setMinimumSize(200, 20);
pHeader->setMaximumSize(200, 20);
QFont font = pHeader->font();
font.setBold(true);
font.setPixelSize(16);
pHeader->setFont(font);
// the content widget
QLabel* plabel = new QLabel("content");
plabel->setMinimumSize(200, 200);
plabel->setMaximumSize(200, 200);
plabel->setStyleSheet("background-color: rgb(182, 182, 182); border: 5px solid black;");
plabel->setAlignment(Qt::AlignCenter);
pBoxLayout->addWidget(pHeader);
pBoxLayout->addWidget(plabel);
pWidget->show();
return a.exec();
}
As you can see here, I've styled the content widget with the use of a Style Sheet. This is really the easiest method, after a bit of practice with them.
The resulting code produces a widget which looks like this: -
You can play with dimensions and fonts to match your original image exactly.

How to get font of widget in Qt set by stylesheet?

I have Qt application with custom stylesheet applied to it (and for all widgets in general) with custom font included in this stylesheet. But when try to get font of some widget font() method return different font. I want to get the font of a QWidget which is set by a stylesheet. The font() method always returns the global system font or the font set by setFont(), but not the font which is set by setStyleSheet() and is used for painting in the widget. I need the font to do some calculations based on the font size. I use Qt 4.6. How can I get real font of widget (that is showing when application run) set by stylesheet?
After some investigations I saw that if I apply defined stylesheet to some widget I can get proper font information (defined by stylesheet) with myWidget->font() method. Also when I set stylesheet to whole MainWindow I can get proper font information with font() method for all the widgets that MainWindow contains. But, when I set stylesheet to instance of QApplication the font() method for all the widgets return default font or the font previously set by setFont(). Why so?
You can retrieve a font of a specific widget reading it's properties, as the following:
//Get pushbutton font.
QFont font = ui->pushButton->property("font").value<QFont>();
qDebug() << font.family() << font.pointSize();
//Get MainWindow font.
QFont font2 = property("font").value<QFont>();
qDebug() << font2.family() << font2.pointSize();
To load values from Qt Stylesheet you should call this methods:
widget->style()->unpolish(widget);
widget->style()->polish(widget);
widget->update();
After this all values of your widget will be updated according your stylesheet values specified.
The best I can tell from QStyleSheetStyle::updateStyleSheetFont, the widget always contains the resolved font from the stylesheet. I'd expect QWidget::font() to return the resolved font that you've set using the stylesheet - i.e. the font that is the merged application font, any parent widget fonts, and the stylesheet font.
The widget must be polished first, of course, unless you're querying after the events have been delivered (i.e. from within the event loop).
// https://github.com/KubaO/stackoverflown/tree/master/questions/style-font-query-test-45422885
#include <QtWidgets>
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QLabel label("Test");
auto font1 = label.font();
label.setStyleSheet("font-size: 49pt;");
label.show();
label.ensurePolished();
auto font2 = label.font();
Q_ASSERT(font1.pointSize() != 49);
Q_ASSERT(font2.pointSize() == 49);
Q_ASSERT(font1.family() == font2.family());
}

QPaintEvent painting region in Qt?

This is a basic doubt regarding the Painting done in Qt.
I have a QScrollArea as my centralWidget in Main Window of my Application. I have added a QFrame frame to the scrollarea. The Layout of the QFrame is QGridLayout.
When I add widgets to the layout like this:
MainWindow::AddLabel()
{
setUpdatesEnabled(false);
QGridLayout *myGrid = (QGridLayout *)ui->frame->layout();
for(int i = 0; i < 1000; i++)
{
QLabel *label = new QLabel();
QString str;
str.SetNum(i);
label->SetText(str);
myGrid->AddWidget(label, 0, i, 0);//add label to i'th column of row 0
}
setUpdatesEnabled(true);
repaint();
}
Please dont worry about the memory leak as it is not the focus of the question.
So my doubt's are:
Is setting the updates disabled during adding widgets to layout any helpful?
Even if I maximise the window not all the QLabel's will be visible to me. So when the code flow leaves the above function & goes to the event loop then are all the QLabel's & the enormous area of QFrame painted? Or only those QLabel's which are visible & only that much area of QFrame which is visible painted?
If you are using a form (.ui) then the widgets inside the ui are not children of your widget MainWindow. Well , setUpdatesEnabled() only affect the current widget as well as its children, so the object ui->frame will still receive updates after myGrid->AddWidget. Change to
ui->frame->setUpdatesEnabled(false);
...
ui->frame->setUpdatesEnabled(true);
Btw, when you enable updates, then screen will be updated. So you dont need to call repaint(); on any widget.

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