Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Picture in center of QTableWidget's cell
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Picture in center of QTableWidget's cell

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 13.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrMNight
    wrote on last edited by
    #1

    Hello. I have QTableWidget. I want to insert picture (without text) in the cell of QTableWidget. Picture must be in the center of cell. When I trying this:
    @
    QTableWidget *sampleTableWIdget = new QTableWidget;
    ...
    QTableWidgetItem *tableItemSmthing = new QTableWidgetItem(QIcon(":/images/icon_sample.png"),"");
    sampleTableWIdget->setItem(1,1, tableItemSmthing);
    @
    But icon appears in the left side of cell (1,1).
    I know method to center widgets in the cell (for ex. QCheckBox):
    @
    QTableWidget sampleTableWIdget = new QTableWidget;
    ...
    QWidget
    wdg = new QWidget;
    QCheckBox checkBox = new QCheckBox(wdg);
    QHBoxLayout
    layout = new QHBoxLayout(wdg);
    layout->setContentsMargins(0,0,0,0);
    layout->addWidget(checkBox);
    layout->setAlignment( Qt::AlignCenter );
    wdg->setLayout(layout);
    sampleTableWIdget->setCellWidget(1,1, wdg);
    @

    but this method didn't work for icons or pixmaps. Can anyone help me?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MrMNight
      wrote on last edited by
      #2

      Problem was solved by using something like this:
      icon.h

      @
      class Icon : public QWidget
      {
      Q_OBJECT
      public:
      Icon(QWidget* obj=0);
      virtual void paintEvent(QPaintEvent*);
      @

      icon.cpp

      @
      Icon::Icon(QWidget* obj): QWidget(obj) {}

      void Icon::paintEvent(QPaintEvent*)
      {
      QPainter p(this);
      QImage img(":/images/icon_sample.png");
      int scaleFactor = 18;
      QImage tempImg = img.scaled(scaleFactor,scaleFactor, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
      p.drawImage(QRectF(width()/2-scaleFactor/2,height()/2-scaleFactor/2,scaleFactor,scaleFactor),tempImg);
      }

      @

      mainwindow:
      @

      QTableWidget sampleTableWIdget = new QTableWidget;
      ....
      Icon
      wdg = new Icon;
      sampleTableWIdget->setCellWidget(1, 1, wdg);
      @

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Why not just use a QLabel ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrMNight
          wrote on last edited by
          #4

          [quote author="SGaist" date="1377683487"]Hi,

          Why not just use a QLabel ?[/quote]

          I din't know about QLabel::setPixmap(..) method, thanks.
          problem can be solved by this:
          @
          QTableWidget *sampleTableWIdget = new QTableWidget;
          ....
          QPixmap pix(":/images/icon_sample.png");
          QPixmap resPix = pix.scaled(17,17, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
          QLabel *lblTest = new QLabel;
          lblTest->setPixmap(resPix);
          lblTest->setAlignment(Qt::AlignCenter);
          sampleTableWIdget->setCellWidget(1, 1, lblTest);
          @

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You're welcome !

            And don't forget to update the thread's title prepending solved so other forum users may know a solution has been found :)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved