PDA

View Full Version : Design User Interface using QT - Help needed



nimrod1989
24th February 2014, 20:26
Hello all,

First of all I apologize if this subject has been already debated on the forums, but I couldn't find anything to solve my issue.

I am using the Visual 2010 Add-In for Qt(Qt Vs. 5.2.0). I want to have something similar to this(click on the below image so you can have a better view about this).
10075

So, as you can see I have a label where I display about three pics. These pics are loaded from a given directory.
I want to be able to load multiple image from the directory but only display three and move through the image list with those buttons(Up and Down). When I load these images, a current image can be selected to be displayed as a preview.

This is my desired behavior.

What I don't know.

What to choose to display the related images from the image list. I've used a QWidget where multiple QLabel have been added. Each label is used to display a image. However, I can't trigger an action in order to be able to have a preview of a given image(currently I'm using a click() Preview button to set the preview of the first image from my list). So, this is not seems to be a good approach.

I read multiple articles about QGraphicsView (http://qt-project.org/doc/qt-4.8/qgraphicsview.html) and QGraphicsScene (http://qt-project.org/doc/qt-4.8/qgraphicsscene.html) and this seeems what I have to use. Unfortunately I am not able to make this working.
I don't know how to implement that, to display those three images and navigate between the image list with Up and Down button.

I would be gratefully if you can share these knowledge with me and all the QT users.

So, how to load multiple images, display only some of them(about 3 of them) in a label, be able to navigate through the image list and be able to display one image from those three as a preview image. Just sent a signal when I select a picture and set the image in the preview label.

If you need more details from me, please do not hesitate to ask.

Thanks a lot,
Danny

sulliwk06
24th February 2014, 21:39
Well I haven't worked a lot with images, but it looks like the easy thing to do would be to turn your QLabels into QPushButtons and use the image as the QIcon for the button.

nimrod1989
25th February 2014, 06:12
Well I haven't worked a lot with images, but it looks like the easy thing to do would be to turn your QLabels into QPushButtons and use the image as the QIcon for the button.

Thank you! Indeed, this can be the easy approach. I didn't tried this yet so I can't tell you how it would look like.

However, I am still waiting for someone who can give me a reply with related guidance for using QGraphicsView and QGraphicsScene, and how it would look like this approach.

Radek
25th February 2014, 08:05
I recommend QToolButton class. The class is intended for toolbars but it is a fully functional "bitmap button" which can display a QIcon without a text. Therefore, 2 QPushButtons with 3 QToolButtons in between and QPixmap (or QImage) to the right. If you click Up or Down, redo the icons. When you click a QToolButton, redo the image.

As to devising the GUI: set sizes of the button and the QIcon, remove text and set style to QToolButtonIconOnly in the Designer.

aamer4yu
25th February 2014, 11:33
So, how to load multiple images, display only some of them(about 3 of them) in a label, be able to navigate through the image list and be able to display one image from those three as a preview image. Just sent a signal when I select a picture and set the image in the preview label.

You could also try for QListWidget / QListView with iconView. You can click the items aka images and do the desired operation. If you fix the grid size of list widget, you may be able to limit the images to three as desired. On button click , you can simply change the current index of the listWidget OR move the scroll bars ...
Also you can have a model with QListWidget which will load images from a given directory which will again ease ur work :)

nimrod1989
25th February 2014, 20:03
Hi all,

Thank you for the valuable information. You give me a starting point. I've just get back to work and I'll let you know how things go.

As soon as I have something working I'll share with you.

Thanks,
Danny

nimrod1989
11th March 2014, 20:35
Hi all,

I'm afraid I can't add a QPixmap as a QlistWidget item. Here's what I've tried:


QString fileNamePics = QFileDialog::getOpenFileName(this, tr("Open File"),
"E:\\Personal\\TestImageLoaderQt",
tr("Images (*.jpg"));

QPixmap imgPixmap (fileNamePics);

QIcon imgIcon(imgPixmap);

myListWidget->setViewMode(QListView::IconMode);

QListWidgetItem *LWidgetItem = new QListWidgetItem(imgIcon, "Image Name", myListWidget,0);

ui.myListWidget->insertItem(1, LWidgetItem);

Unfortunately, nothing is displayed.

Radek
12th March 2014, 07:31
Yes, it is not.

(1) If you specify the parent in the QListWidgetItem ctor (it must be a QListWidget *), the item will be added to the parent widget. The widget becomes the owner of the item. Therefore, adding the widget again using QListWidget::insertItem() is wrong. Comment out the insertItem() line and see.

(2) You gain more flexibility, if you create "stand alone" items, which you add to the widget subsequently by addItem()/insertItem(). Therefore:


QListWidgetItem *LWidgetItem = new QListWidgetItem(imgIcon, "Image Name");
ui.myListWidget->insertItem(0, LWidgetItem);


Note also, that rows are numbered from zero and that you can insert only before an existing item. Adding an item into an empty widget: either add or insert at position 0. Anyway, I don't think you get what you want. You get an "icon view" of a list widget rather than bitmap buttons.

nengtyas
12th March 2014, 10:56
I propose QToolButton type. This type is supposed for toolbars however this is a thoroughly well-designed "bitmap button" which will show a QIcon with no textual content. Therefore, 3 QPushButtons along with 3 QToolButtons between as well as QPixmap (or QImage) for the correct. Should you just click In place or maybe Straight down, redo the icons. Once you just click a QToolButton, redo the impression.
http://watchfree.me/42/w.png

nimrod1989
16th March 2014, 19:44
I propose QToolButton type. This type is supposed for toolbars however this is a thoroughly well-designed "bitmap button" which will show a QIcon with no textual content. Therefore, 3 QPushButtons along with 3 QToolButtons between as well as QPixmap (or QImage) for the correct. Should you just click In place or maybe Straight down, redo the icons. Once you just click a QToolButton, redo the impression.
http://watchfree.me/42/w.png

Thank you all for the help. Unfortunately, I can't get this working as I need. I can't get it working at all.
Can you please write a few lines of code in order to get a starting point?

Added after 15 minutes:

What is the container for my QToolButton? How can I add it?

Radek
16th March 2014, 20:12
The frame. It's a button like any other button. Place three QToolButtons on the frame and set their sizes. You can add icons from your app.

nimrod1989
18th March 2014, 20:53
Ok, I've done this.


QString fileNamePics = QFileDialog::getOpenFileName(this, tr("Open File"),
"E:\\Personal\\TestImageLoaderQt",
tr("Images (*.jpg"));

QPixmap imgPixmap (fileNamePics);
QIcon imgIcon(imgPixmap);
ui.pushButtonUp->setIcon(imgIcon);
ui.pushButtonUp->setIconSize(QSize(100, 80));

Here is how it looks like:
10139
Unfortunately, the iconSize seems to depend on the image which I'm loading. The IconSize is not set as expected with the related values (100, 80). It depends the image I'm loading, its width and height.
My pushButtons have this size(100, 80); It seems that I need to scale this QIcon on my pushButton. I didn't find any functions which implements this.

Does anybody have another approach for this? Maybe I should try to use something else, instead my pushButtons?

Radek
19th March 2014, 03:40
(1) Scaling pixmaps: see QPixmap::scaled()
(2) When you setIcon() the icon is displayed on the button. The subsequent setIconSize() will change nothing.

Therefore:


QPixmap imgPixmap(fileNamePics);
QIcon imgIcon(imgPixmap.scaled(100,80));

ui.PushButtonUp->setIcon(imgIcon);

Danediason
21st March 2014, 05:51
Hallo everyone,
I like this form, place tell me why use design QT ?
thank,s

nimrod1989
23rd March 2014, 07:55
(1) Scaling pixmaps: see QPixmap::scaled()
(2) When you setIcon() the icon is displayed on the button. The subsequent setIconSize() will change nothing.

Therefore:


QPixmap imgPixmap(fileNamePics);
QIcon imgIcon(imgPixmap.scaled(100,80));

ui.PushButtonUp->setIcon(imgIcon);


I want to thank you for all the help. Unfortunately, using a QIcons on the related buttons doesn't fulfill my expectations. First of all, I cannot cover a QButton without distorting the image.

So, I would like to have a different approach. Does anybody have any idea what I could try?

Radek
23rd March 2014, 09:00
OMG! The buttons have some sizes. The icons, as I have seen, have another sizes. Something needs to be "distorted", this cannot be helped. Either prepare "undistorted" icons for the buttons in advance (use some image processor, for example GIMP, for it) or use QListView in Icon mode. In the later case, you get an "icon view", as you know it from file managers, where you can place icons with different sizes. I am afraid, there is no other acceptable solution.

nimrod1989
23rd March 2014, 13:27
Hi Radek,

I think I have something which is pretty much what I've wanted. Here's how it looks like:

10161

So, when I double click on an item from my QListView, the related item will be displayed on my central label. The Up and Down buttons will be deleted from the UI since there is no need of them (you can navigate through the QListWidget using its scroolBar).

Here's my code


QStringList listaPics = QFileDialog::getOpenFileNames(this, tr("Open File"),
"E:\\Personal\\TestImageLoaderQt",
tr("Images (*.jpg"));

//get the pixList
for(int i = 0; i < listaPics.size(); i ++)
{
QPixmap imgPixmap (listaPics[i]);
pixList.append(imgPixmap);
}

for (int i = 0; i < pixList.size(); ++i)
{
QPixmap pix(pixList[i]);

QIcon *iconImage = new QIcon(pix);

QListWidgetItem *listWindgetItemImage = new QListWidgetItem;
listWindgetItemImage->setIcon(*iconImage);

ui.listWidgetImage->addItem(listWindgetItemImage);
ui.listWidgetImage->setIconSize(QSize(70, 80));
}

When an item is doubleClicked(), I update my central Qlabel which display the image in a preview mode. Unfortunately, I need somehow to scale my label which holds my image in order to be properly displayed (not the image to be scaled in order to fit in my QLabel. The QLabel needs to be scaled upon the image sizes).
Have any tips about this?

Radek
23rd March 2014, 14:43
If I understand well, the central (picture displaying) widget is a QLabel and it scales your image. You do not want the image scaled. If so then make the central widget a QWidget and create a QPainter in it. If you need to draw an image, then "erase background" of the QWidget by QPainter::fillRect() and then draw the image at a given point (it depends on the sizes of the image) by QPainter::drawImage(). You will need QImages instead of QPixmaps now.

nimrod1989
27th March 2014, 20:25
Hi Radek,

Thank you for your help. I got this working using the QLabel->resize() function in order to properly display a scaled image.