QScroll bar range not setting properly - qt

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.

Related

Application crashing when searching for QLineEdit in QFormLayout

I've experienced a weird crash when trying to find a QLineEdit in a QFormLayout.
Firstly, I created a QFormLayout and set a name for it:
QFormLayout *charColLayout = new QFormLayout; charColLayout->setObjectName("charColLayout");
Then, I created a QLineEdit, modified it a bit and add it in to the layout, and I also give it a name:
QLineEdit *delim = new QLineEdit;
delim->setMaxLength(1);
delim->setMaximumWidth(100);
delim->setText("/");
delim->setObjectName("delEdit");
charColLayout->addRow("Delimiter", delim);
Afterward, in a completely different function, I re-searched that layout with findChild():
QFormLayout *charcoal = secondHoriField->findChild<QFormLayout *>("charColLayout", Qt::FindChildrenRecursively);
It should be noted that secondHoriField is just a normal QLayout which my QFormLayout is located in.
Finally, I attempted to find that QLineEdit:
QLineEdit *delimEdit = charcoal->findChild<QLineEdit*>("delEdit", Qt::FindChildrenRecursively);
if (delimEdit == nullptr) {cerr << "error\n";} //debug
string curDelim = qStrToStr(delimEdit->text());
And it surprisingly came down with a crash, and as the output shown, it's because the delimEdit is null.
18:06:10: Starting D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe ...
error
18:06:17: The program has unexpectedly finished.
18:06:17: The process was ended forcefully.
18:06:17: D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe crashed.
But when I switched the findChild() function for this rather bruteforce-y line:
QLineEdit *delimEdit = dynamic_cast<QLineEdit*>(charcoal->itemAt(1)->widget());
cerr << qStrToStr(delimEdit->objectName()) << endl; //debug line
The program ran fine, and it showed the same name I set for the QLineEdit:
18:12:02: Starting D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe ...
delEdit
18:12:11: D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe exited with code 0
Why did this happened?
Another note: qStrToStr() is a function I implement to convert QString to std::string, and I have hand-checked it.
While findChild is a QObject method the itemAt is a QFormLayout method.
QObject::findChild
QFormLayout::itemAt
With addRow you add an item to the QFormLayout. This does not make it a child in the context of the QObject.
QFormLayout::addRow
The purpose of the QFormLayout is to organize the positioning of QWidgets, it is not meant to serve as a container. Maybe you could check whether the top level QWidget (e.g. QMainWindow) holding the QFormLayout would be a better choice as a parent for the QLineEdit.
Assuming you have some kind of QMainWindow:
QMainWindow myMainWindow;
// ...
QLineEdit *delim = new QLineEdit(&myMainWindow);
delim->setObjectName("delEdit");
//...
In another location:
auto delimEdit = myMainWindow.findChild<QLineEdit*>("delEdit");

Qt: How to Find the Position of the Mouseclick Relative to an Image

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)

QGraphicsVideoItem in QGraphicsView

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);

Segmentation fault when accessing the text of QLabel

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"));

Transparent image in QT

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);

Resources