Results 1 to 3 of 3

Thread: Creating a list with icon and text in the center of icon

  1. #1
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Creating a list with icon and text in the center of icon

    in the ui you can see the style sheet its working perfect for making the icon show and having the text but i need to move ONLY the text so it don't lay ontop of the image border please help me out i cant seem to get the text to move over to the right about 6px

    UI
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MyDelegate</class>
    4. <widget class="QWidget" name="MyDelegate">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>352</width>
    10. <height>301</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MyDelegate</string>
    15. </property>
    16. <widget class="QListView" name="listView">
    17. <property name="geometry">
    18. <rect>
    19. <x>0</x>
    20. <y>0</y>
    21. <width>351</width>
    22. <height>301</height>
    23. </rect>
    24. </property>
    25. <property name="styleSheet">
    26. <string notr="true"> QListView {
    27. show-decoration-selected: 1; /* make the selection span the entire width of the view */
    28. border:transparent;
    29. color: black;
    30. font: 12pt Trebuchet MS;
    31. }
    32.  
    33. QListView::text {
    34. margin-left: 24px;
    35. }
    36.  
    37. QListView::item {
    38. image: url(:/images/directory.png);
    39. }
    40.  
    41. QListView::item:alternate {
    42. background: #EEEEEE;
    43. }
    44.  
    45. QListView::item:selected {
    46. image: url(:/images/directory-hit.png);
    47. }
    48.  
    49. QListView::item:selected:!active {
    50. image: url(:/images/directory-hit.png);
    51. }
    52.  
    53. QListView::item:selected:active {
    54. image: url(:/images/directory-hit.png);
    55. }
    56.  
    57. QListView::item:hover {
    58. image: url(:/images/directory-hit.png);
    59. }</string>
    60. </property>
    61. </widget>
    62. </widget>
    63. <layoutdefault spacing="6" margin="11"/>
    64. <resources/>
    65. <connections/>
    66. </ui>
    To copy to clipboard, switch view to plain text mode 

    CPP
    Qt Code:
    1. #include "mydelegate.h"
    2. #include "ui_mydelegate.h"
    3. #include <QListView>
    4.  
    5. MyDelegate::MyDelegate(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::MyDelegate)
    8. {
    9. ui->setupUi(this);
    10.  
    11. setAttribute(Qt::WA_Hover, true);
    12.  
    13. model = new QStandardItemModel(5, 2, this);
    14. //ui->listView->setStyleSheet("border:transparent; background:transparent; color: rgb(160, 160, 160); font: 12pt Trebuchet MS");
    15. ui->listView->setViewMode(QListView::ListMode);
    16. ui->listView->setWrapping(FALSE);
    17.  
    18.  
    19. for( int r=0; r<5; r++ )
    20. for( int c=0; c<2; c++)
    21. {
    22. //QStandardItem *item = new QStandardItem( QString("L: %0").arg(r) );
    23. QStandardItem* item = new QStandardItem(QString("L: %0").arg(r));
    24. //item->setIcon( QIcon ( ":/images/directory.png" ));
    25. item->setEditable(false);
    26. item->setCheckable(false);
    27. item->setSizeHint(QSize(345,42));
    28.  
    29. model->setItem(r, item);
    30. }
    31.  
    32. //Setting the icon size
    33. ui->listView->setIconSize(QSize(345,42));
    34. //Setting the model
    35. ui->listView->setModel( model );
    36.  
    37. }
    38.  
    39. MyDelegate::~MyDelegate()
    40. {
    41. delete ui;
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by prophet0; 12th December 2011 at 21:06. Reason: new code and updated help!

  2. #2
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating a list with icon and text in the center of icon

    Sorry i didnt have my image in the resource file .... stupid move..

    so i just need help with aligning the text over the image

  3. #3
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating a list with icon and text in the center of icon

    any suggestions ?

Similar Threads

  1. Creating an Icon-Editor
    By Nedec in forum Qt Programming
    Replies: 5
    Last Post: 22nd March 2012, 06:58
  2. Replies: 1
    Last Post: 4th April 2011, 19:41
  3. Creating moving icon set
    By avanindra in forum Qt Programming
    Replies: 0
    Last Post: 9th January 2011, 18:32
  4. drop-down action icon list ?
    By divide in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2010, 08:57
  5. qstandarditem how sisplay icon in center?
    By yunpeng880 in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2009, 09:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.