i am new to QT, i got to know how to load a simple image on a window.
i want to know how to make transparent?.
please tell me the way to achieve it.
Thanks
This is how I did it:
canvas = new QImage(":/Zuerich.jpg");
city = new QImage(canvas->size(),QImage::Format_ARGB32);
QPainter p(city);
p.setOpacity(0.1);
p.drawImage(0,0,*canvas);
p.end();
// the proof:
QRgb pix = city->pixel(10,10);
qDebug() << "Alpha" << qAlpha(pix);
Related
I would like to implement an image editor. I have a QPixmap in a QLabel in a QHBoxLayout. I have overriden the mousePressEvent in the parent widget. When the mousePressedEvent occurs, the
event->pos() = QPoint(271,115)
points to a place which is displaced relative to the pointer (mouse). The displacement is the distance of the QLabel from the QWidget's corner. It gets bigger when I resize the window. How do I find this displacement vector? I want to draw a pixel on the QPixmap exactly where the mouse is.
Note that the following methods give no remedy:
qDebug() << "event->pos()" << event->pos();
qDebug() << "this->pos() = " << this->pos();
qDebug() << "pm_imageLabel->pos() =" << pm_imageLabel->pos();
qDebug() << "pos = " << mapFromGlobal(QCursor::pos());
These give all different positions. No searching on the internet or in Qt's documentation brought me closer to the answer. Thank You in advance.
Finally I have figured it out partially with the help of vahancho. The the position of the QPixmap withing QLabel is difficult to determine, but I can forbid QLabel to resize. So I set the size of QLabel to the image size.
pm_imageLabel->setPixmap(m_pixmap);
pm_imageLabel->setFixedSize(m_pixmap.size());
and I override the mousePressed even inside QLabel class. This way the event->pos is correct.
Thanks.
I know it's been a while but i found a solution that works without having to resize the QLabel.
The solution is in Python.
label = QLabel(...)
img_pix = QPixmap(...)
label.setPixmap(img_pix)
# now you can get mouse click coordinates on the label by overriding `label.mousePressEvent`
# assuming we have the mouse click coordinates
coord_x = ...
coord_y = ...
# calculating the mouse click coordinates relative to the QPixmap (img_pix)
img_pix_width = img_pix.width()
img_pix_heigth = img_pix.height()
label_width = label.width()
label_height = label.height()
scale_factor_width = label_width / img_pix_width
scale_factor_height = label_height / img_pix_heigth
relative_width_in_img_pix = coord_x / scale_factor_width
relative_height_in_img_pix = coord_y / scale_factor_height
relative_coordinates_in_img_pix = QPoint(relative_width_in_img_pix, relative_height_in_img_pix)
I simplified the code and it is like this:
Mat mat = imread("xxx.jpg"); //Successfully read the image, confirmed by cvShowImage.
if (mat.empty())
{
qDebug() << "Couldn't load image";
return;
}
Mat cpy = mat.clone();
cvtColor(mat,cpy,CV_BGR2RGB);
QImage image(cpy.data, cpy.cols, cpy.rows, cpy.step, QImage::Format_RGB888);
try {
pm = QPixmap::fromImage(image); //crash line
} catch(std::exception const &ex){
qDebug()<<ex.what();
}
however the program just crashed without any debug log.. I've tried many images and the result is same. I tried to find the "stack trace" and it seems give segfault on this..
Had similar crash in QPixmap.fromImage. Found workaround by resizing image to a size aligned to 4 bytes
aligned = cv2.resize(img, (img.shape[1]//4*4, img.shape[0]//4*4), fx=0, fy=0, interpolation=cv2.INTER_NEAREST)
rgb = cv2.cvtColor(aligned, cv2.COLOR_BGR2RGB)
qimage = QImage(rgb.data, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
okay.. just change the conversion code to
QImage image(cpy.data, cpy.cols, cpy.rows, cpy.step, QImage::Format_RGB888);
image = image.rgbSwapped();
solved it. Actually I don't know why this fixed it. Maybe because I should use COLOR_BGR2RGB rather than CV_BGR2RGB..
I am trying to set the QScrollbar range using QScrollBar::setrange(min, max). But the values are not reflected when the app is run.
This is the code called from MainWindow constructor.
ui->setupUi(this);
QScrollArea* scroll = new QScrollArea(this);
scroll->setGeometry(ui->frame->geometry());
scroll->setWidget(ui->frame);
scroll->verticalScrollBar()->setRange(0, 1000);
qDebug() << "Max is " << scroll->verticalScrollBar()->maximum();
scroll->show();
PFA the images
Am I missing something. Any leads would be helpful. Thanks in advance.
I am trying to draw video inside QGraphicsView in QT5.5. Here's the code
QString path = "video.mp4";
qDebug() << QFile::exists(path);
ui->graphicsView->setScene(new QGraphicsScene());
QMediaPlayer pl;
QGraphicsVideoItem vid;
pl.setVideoOutput(&vid);
qDebug() << pl.error();
vid.setSize( QSizeF(1920, 1080) );
pl.setMedia( QUrl::fromLocalFile( path ) );
qDebug() << pl.error();
ui->graphicsView->scene()->addItem(&vid);
ui->graphicsView->resize(1920, 1080);
pl.play();
qDebug() << pl.error();
Nothing is drawn. No errors. File exists. Maybe some issue with plugins? Or hardware acceleration, cause I am on notebook with discrete card. Thank you in advance.
Ok, I got it. My stupid mistake. QMediaPlayer and QGraphicsVideoItem was deleted after exiting scope.
In case someone else runs into this page. You may have to use a QVideoWidget instead of a QGraphicsVideoItem.
https://whynhow.info/30713/How-to-make-friends-QCamera-and-QGraphicsVideoItem
// Note: QGraphicsVideoItem doesn't work but QVideoWidget does
// https://whynhow.info/30713/How-to-make-friends-QCamera-and-QGraphicsVideoItem?
auto *vidWidget = new QVideoWidget;
mpScene->addWidget(vidWidget);
mpCamera = new QCamera(acCamera, this);
mpCamera->setViewfinder(vidWidget);
mpCamera->setCaptureMode(QCamera::CaptureVideo);
I got a problem with the QLabel. I got a QtWidget with a QLabel inside. Now I want to change the text of the Label with following code:
QLabel* safetyLabel = this->findChild<QLabel *>("safety_bits");
safetyLabel->setText(QString("test"));
printf("%i", (safetyLabel->text()).length());
but I always get a "Segmentation fault". I think it's something quite simple, but I just can't see it...
Any ideas?
Your safetyLabel can be NULL if you use QtCreators' designer to build your UI and execute your code before you call ui->setupUi(this); in MainWindows constructor.
Here is the code.
QLabel *safetyLabel = NULL;
safetyLabel = (QLabel *) this->findChild("safety_bits");
if(!safetyLabel)
{
qDebug() << "Failed to find safety_bits label!";
return 1;
}
safetyLabel->setText(QString("safety_bits is here"));