I want to add image to the items in the table view



  • Hi All,
    i want to have a control of tableview format to list many images on a single item value.
    So shall i go with QTableView or QTableWidget.?...which is the best & easiest way to do that?

    Please help me..



  • Please try to find a difference between these two, try to implement something and if you have any problems then let us know.
    You can start from the examples from qt source code e.g. spinboxdelegate.



  • [quote author="M_31" date="1310140082"]Hi All,
    i want to have a control of tableview format to list many images on a single item value.
    So shall i go with QTableView or QTableWidget.?...which is the best & easiest way to do that?
    [/quote]
    What do you mean by the highlighted part of your question exactly? Do you need to put more than one image in a cell of a table? If so, than neither QTableView nor QTableWidget is going to help you do that directly. Both are only able to display a single image in a cell normally. If you want to go beyond that, you'll need to do a bit of programming yourself.



  • Andre...

    I have used QTableWidgetItem under QTableWidget to add more icons into the particular Item ..but my application is getting crash ..plz let thro some light on this.. i am very new to this QT.
    @
    QTableWidgetItem *item;
    item[0]=QTableWidgetItem(QIcon("C:\db.ico"),"",1);
    item[1]=QTableWidgetItem(QIcon("C:\db.ico"),"",1);
    item[2]=QTableWidgetItem(QIcon("C:\db.ico"),"",1);

    QTableWidget tableWidget(1, 1);
    tableWidget->setItem(row, 0, item);
    @
    its throwing some error like..
    @
    Application.exe exited with code -1073741819
    @

    Edit: Please use @ tags around code sections; Andre


  • Moderators

    M_31, please wrap your code in @ tags.



  • Are you sure the above even compiles? You are creating items on the stack instead of the heap, and you are setting an array where a pointer to a single item is expected (yes, I know that arrays work like pointers...). And yet you are suprised that it doesn't work?

    In a single position in a tableWidget, you can only put a single QTableWidgetItem, and that item has to be on the heap. So, putting multiple images in a single cell will have to be done differently. Basically, you have two options:

    Merge your images into a single image (for instance by painting the images on a single new QImage), or

    Create your own delegate, put the different images in different roles or in some kind of other data structure you can put in a QVariant, and let that delegate render all the images in a single cell.



  • it sounds good..

    Can i have any sample application site to have some idea abt this merging images into a single image or creating own delegates to put multiple images??

    Plz help me on this..



  • Please take a look at my first entry in this thread.



  • [quote author="M_31" date="1310551796"]it sounds good..
    Can i have any sample application site to have some idea abt this merging images into a single image or creating own delegates to put multiple images??
    [/quote]
    I don't know of sample applications, but the documentation of [[Doc:QImage]] and [[Doc:QPainter]] are not so hard to understand. What it comes down to is:

    Load the images you want to display into separate QImage instances

    Calculate the size needed for an image that can contain them all

    Create a new QImage of the size calculated in step 2, and fill it with the background color you want

    Open a QPainter on the new image, and use an appropriate ::drawImage() overload to draw the separate images into your One-Image-To-Rule-Them-All(TM).

    Set the newly drawn image as the image to draw in your QTableWidget cell.

    That's only a rough outline, of course, but it should get you started I think. Give it a serious try, and if you have any questions on this that you can't figure out by reading through the docs, please do come back and ask in this topic.

    And please: don't open a new topic on the same issue.



  • Thanks Andre


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.