Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Spanish
  4. Stylesheet en Delegate

Stylesheet en Delegate

Scheduled Pinned Locked Moved Unsolved Spanish
2 Posts 2 Posters 969 Views
  • 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.
  • R Offline
    R Offline
    raulgg
    wrote on last edited by
    #1

    Hola, he creado un QStyledItemDelegate para mostrar una imagen en un QTableView a la cual la tengo aplicada un stylesheet, todo funciona bien a excepción que los items mostrados desde el delegate ya que no muestran el stylesheet asignado a la tabla. ¿Alguien me puede ayudar?

    Stylesheet

        qApp->setStyleSheet("QHeaderView::section "
                            "{background-color: rgb(255, 255, 255); "
                            "padding: 4px; "
                            "font: 12pt 'Courier New'; "
                            "border-style: none; "
                            "border-bottom: 1px solid; "
                            "border-bottom-color: rgb(160, 208, 235);} "
                            "QTableView::item"
                            "{padding: 4px;"
                            "border-bottom: 1px solid;"
                            "border-bottom-color: rgb(186, 186, 186);}");
    

    El delegate

    ///.h
    class ImagenDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        ImagenDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
        void paint(QPainter *painter, const QStyleOptionViewItem &option,
                   const QModelIndex &index) const override;
    };
    
    /// .ccp
    
    void ImagenDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                               const QModelIndex &index) const
    {
        if (index.column()==2){
            QDir aplication_path;
            QString value = index.model()->data(index, Qt::EditRole).toString();
            QString path_imagen = aplication_path.currentPath() + "/imagenes/productos/" + value;
            QFileInfo check_file(path_imagen);
            QStyleOptionButton button;
            QRect r = option.rect;
            int x,y,w,h;
            x = r.left() + r.width() - 57;
            y = r.top()+4;
            w = 54;
            h = 54;
            button.rect = QRect(x,y,w,h);
            button.state = QStyle::State_Enabled;
            button.iconSize = QSize(48,48);
            if (check_file.exists() && check_file.isFile()){
                button.icon = QPixmap(path_imagen);
            } else {
                button.icon = QPixmap(":/img/img/logo.png");
            }
            qApp->style()->drawControl(QStyle::CE_PushButton, &button, painter);
            //qDebug()<<option.widget->styleSheet();
            //QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
        }
    }
    
    juankiJ 1 Reply Last reply
    0
    • R raulgg

      Hola, he creado un QStyledItemDelegate para mostrar una imagen en un QTableView a la cual la tengo aplicada un stylesheet, todo funciona bien a excepción que los items mostrados desde el delegate ya que no muestran el stylesheet asignado a la tabla. ¿Alguien me puede ayudar?

      Stylesheet

          qApp->setStyleSheet("QHeaderView::section "
                              "{background-color: rgb(255, 255, 255); "
                              "padding: 4px; "
                              "font: 12pt 'Courier New'; "
                              "border-style: none; "
                              "border-bottom: 1px solid; "
                              "border-bottom-color: rgb(160, 208, 235);} "
                              "QTableView::item"
                              "{padding: 4px;"
                              "border-bottom: 1px solid;"
                              "border-bottom-color: rgb(186, 186, 186);}");
      

      El delegate

      ///.h
      class ImagenDelegate : public QStyledItemDelegate
      {
          Q_OBJECT
      public:
          ImagenDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
          void paint(QPainter *painter, const QStyleOptionViewItem &option,
                     const QModelIndex &index) const override;
      };
      
      /// .ccp
      
      void ImagenDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
      {
          if (index.column()==2){
              QDir aplication_path;
              QString value = index.model()->data(index, Qt::EditRole).toString();
              QString path_imagen = aplication_path.currentPath() + "/imagenes/productos/" + value;
              QFileInfo check_file(path_imagen);
              QStyleOptionButton button;
              QRect r = option.rect;
              int x,y,w,h;
              x = r.left() + r.width() - 57;
              y = r.top()+4;
              w = 54;
              h = 54;
              button.rect = QRect(x,y,w,h);
              button.state = QStyle::State_Enabled;
              button.iconSize = QSize(48,48);
              if (check_file.exists() && check_file.isFile()){
                  button.icon = QPixmap(path_imagen);
              } else {
                  button.icon = QPixmap(":/img/img/logo.png");
              }
              qApp->style()->drawControl(QStyle::CE_PushButton, &button, painter);
              //qDebug()<<option.widget->styleSheet();
              //QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
          }
      }
      
      juankiJ Offline
      juankiJ Offline
      juanki
      Moderators
      wrote on last edited by
      #2

      Hola @raulgg
      Dices que "los items mostrados desde el delegate no les afecta el estilo". O sea, que hay otros en la misma tabla que sí les afecta.

      ¿Has probado aplicando el estilo en el delegate? Supongo que el estilo lo aplicas antes de cargar los datos de la tabla ¿no? No sé si esto podrá afectar a que se apliquen los estilos o no.

      Imagino que te has mirado la documentación por si hay algún detalle que afecte a los delegates.

      Un saludo

      Intenta explicar el problema lo más claro y detallado posible. Adjunta los errores y el código que creas da el error.
      Procura escribir correctamente y sin faltas de ortografía.

      Si la duda se solucionó, por favor, marca el tema como 'solucionado'.

      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