How to Stretch Bitmap into Parent Panel (wxWidgets Custom) - c++

Screenshot of Panel and a custom inside of it
I set a wxButton and it loads bitmap,png formats to Panel at right. But image is overfitting. I want to stretc image to Panel when it's loaded by button.
Here is button's function, just in case:
void rrFrame::testClick(wxCommandEvent& event)
{
wxBitmap bmp;
wxString strPath = wxT("img\\img1.png");
bmp.LoadFile(strPath, wxBITMAP_TYPE_ANY);
camFrame_wx->DrawBitmap(bmp, wxPoint(0, 0), false);
//camFrame_wx is the variable name of 'Custom'
}
I suppose i need a stretch or fit function in constructor. How to do this?

I think the easiest way to do this would be to load the image file into a wxImage first, then rescale the wxImage, and finally convert the wxImage to a wxBitmap. Like so:
void rrFrame::testClick(wxCommandEvent& event)
{
wxString strPath = "img\\img1.png";
wxImage im(strPath,wxBITMAP_TYPE_ANY );
wxSize sz = camFrame_wx->GetSize();
im.Rescale(sz.GetWidth(),sz.GetHeight(),wxIMAGE_QUALITY_HIGH );
wxBitmap bmp(im);
camFrame_wx->DrawBitmap(bmp, wxPoint(0, 0), false);
//camFrame_wx is the variable name of 'Custom'
}
Two additional comments:
You shouldn't need the wxT macro for string literals in wxWidgets 3.0 or later.
You can use other options such as wxIMAGE_QUALITY_NEAREST instead of wxIMAGE_QUALITY_HIGH if you need faster rescaling. The full list is available here.

Related

How to overlay 2 Images in QT

I have 2 images that I load in QImage and my question is :
How can I overlay these 2 images in the simplest way, to then save it in a QPixmap ?
Here are the following images :
And the awaited result :
(The final image will be used in a QTableView to show the user if it has more of the same potion)
I was able to find the answer on my own after some more research.
Here is my code that I came up with (if you can do it better I would like to see it because I would like to implement any better approach if there is any):
QPixmap base = QPixmap::fromImage(QImage("Your 1st img"));
QPixmap overlay = QPixmap::fromImage(QImage("your 2nd img"));
QPixmap result(base.width(), base.height());
result.fill(Qt::transparent); // force alpha channel
{
QPainter painter(&result);
painter.drawPixmap(0, 0, base);
painter.drawPixmap(0, 0, overlay);
}
QStandardItem *pCombinedItem = new QStandardItem(); //this variable should be in the .h file if you want to conserve it further on.
pCombinedItem->setData(QVariant(result), Qt::DecorationRole);//adding the final img into the StandardItem which we can put then into our table after we put it into a StandardItemModel like so :
model->setItem(1,4,pCombinedItem);
pInventory->setModel(model); //and we can put our model into our tableview

Resizing a QPixmap inside a QLabel according to the Window

In my window class, inheriting of QMainWindow, I have a QLabel containing a QPixmap, updated every 20ms.
I want the QLabel, and the QPixmap inside it, to be resized according to the resizing of the window.
I want this Central Widget to take as much space as neccessary but also to be able to resize it down. Even smalled than the original size. But always keeping the ratio.
The actual code :
// in the window constructor
this->setWindowFlags(Qt::Window);
this->resize(500, 300);
this->setCentralWidget(this->label);
// in the updating function
QPixmap output;
output = output.fromImage(Mat2QImage(theImage));
this->label->setPixmap(output);
Now I've tried with :
output.scaled(this->label->x(), this->label->y(), Qt::KeepAspectRatio)
but it doesn't work ...
How can I do that ?
EDIT : I'm using Qt 5.3
QPixmap::scaled is const. Next code doesn't work:
// in the window constructor
this->setCentralWidget(this->label);
// in the updating function
QPixmap output;
output = output.fromImage(Mat2QImage(theImage));
output.scaled( this->label->x(), this->label->y(), Qt::KeepAspectRatio );
this->label->setPixmap(output);
Because output doesn't change. Maybe you need something like this:
// in the window constructor
this->setCentralWidget(this->label);
// in the updating function
QPixmap output;
output = output.fromImage(Mat2QImage(theImage));
output = output.scaled( this->label->x(), this->label->y(), Qt::KeepAspectRatio );
this->label->setPixmap(output);
Try puting your QLabel inside a Layout.
hl = new QHBoxLayout;
hl->addWidget(label);
centralWidget()->setLayout(hl);
check this out: http://qt-project.org/doc/qt-4.8/layout.html

wxStaticBitmap is not reflecting parent back ground gradiation with style wxTRANSPARENT_WINDOW instead of that its showing parent back groung colour

I have created a panel then in paint event i am doing gradiation , On top of panel i am creating wxstaticbitmap with style wxTRANSPARENT_WINDOW ,but background of staticbitmap showing parent backgroung colour instead of gradient colour , could someone please help me out of this ,Here is my code
NOTE: i am using png type images
Code:
MWpanel::MWpanel()
{
wxbitmap bitmapPNG
wxStaticBitmap *bit = new wxStaticBitmap(this ,-1 ,bitmapPNG ,wxDefaultPosition ,wxDefaultSize ,wxTRANSPARENT_WINDOW)
}
MWpanel::OnPaint(wxPaintEvent &event)
{
wxColour col1
wxColour col2
wxBufferedPaintDC dc(this);
dc.GradientFillLinear(wxRect(0, 0, this->GetSize().GetWidth(), this->GetSize().GetHeight()), col1, col2, wxSOUTH);
}
You need to use SetBackgroundStyle(wxBG_STYLE_TRANSPARENT) and not wxTRANSPARENT_WINDOW. See the "erase" sample included in wxWidgets for some example code.

Qt - Pop up a bubble when mouse is over QRect object

My QRect object is a fixed-sized containter of plain text, when there is too much text I would truncate the text and trail ... at the end. For example, Longlonglonglong is truncated to Longlong.... But I want to display the full-length text in a bubble when mouse pointer is over the rect.
The bubble is like the Go to Google Home:
Is this possible ?
Unfortunately QPainter can't do that for you, the drawText(..) flags don't support it. Thankfully, you can pre-elide the text for it ("eliding" is where you truncate with an elipsis) using QFontMetrics:
QFontMetrics fontM( QApplication::font() );
QRect r( 0, 0, 30, 10 );
QString text = "Longlonglonglong";
QString elidedText = fontM.elidedText( text, Qt::ElideRight, r.width() );
painter->drawText( r, Qt::AlignLeft, elidedText );
When you say "text in a bubble when mouse pointer is over", I presume you mean a tooltip - in which case implement it for the widget as normal and give the full text rather than the elided.

Qt: heightForWidth for word-wrapped text

I have a box with a varying width and a word-wrapped text. I need to set new height every time user changes box's width. The box is displayed by QPainter inside paintEvent(QPaintEvent *) function. There is several solutions, for example current (not very smart, i do this in resizeEvent(QResizeEvent *)):
unsigned new_height = 0; // the height i want to find out.
unsigned given_width = width();
QPainter painter (this); // i need painter, because i want to ask it's default font.
QLabel lab; // the widget that can do word-wrap.
lab.setText( "A word wrapped text" ); // the text
lab.setFont( painter.font() ); // set QPainter's default font.
lab.setWordWrap( true ); // enable word-wrap
new_height = lab.heightForWidth( given_width ); // tada! :)
But the code is overkill:
1) Creading QPainter is not good outside paintEvent(QPaintEvent *);
2) BUT i need QPainter to request what font is default for it to ask metrics for that font.
Should i change my code and do this operation with help of QPainter::boundingRect() inside the paintEvent(QPaintEvent *) function? But i'd like to reduce CPU consumption inside the paintEvent(QPaintEvent *) and calculate new height only when width changed, but not every time it displayed.
What is other solutions for the purpose of subject? QFontMectircs?
I think you have the right idea of using QFontMetrics. The whole idea of the class is to assist the situations like you have here. Take a look at QFontMetricsF::boundingRect()
Use your target paint rectangle as the input rect, but set the height to the max that your widget height. I'd just put something like INT_MAX in it just to be sure. :)