使用自定义QHeaderView

时间:2016-04-01 21:32:00

标签: c++ qt qheaderview

我想设置一个自定义QHeaderView来旋转水平标题的文本。我正在使用QStandarItemModel

目前我已经

class QHeaderViewR : public QHeaderView
{
public:
    QHeaderViewR():QHeaderView(Qt::Horizontal)
    {}

    void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    {
      QPen pen(Qt::black);
      painter->setPen(pen);
      painter->translate(rect.width() * logicalIndex, (logicalIndex * -18) -18);
      painter->rotate(90);
      painter->drawText(rect,"header");
    }
};

我真的不明白我对翻译做了什么。我只是试验和错误,直到它与列有些匹配。它仍然没有完美地做到这一点,它没有明显的理由削减文本。我应该怎么做才能使文字与列匹配而不被切断?

"pic of the mismatch and cut text"

另一件事是我不想只在每一列上写“标题”。要查看的模型将Horizo​​ntalHeaderItem分配给每一列,我想显示这些标题

提前感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我解决了。只需添加一个QStringList作为构造函数的参数,并使用逻辑索引迭代它。这是最终结果

class QHeaderViewR : public QHeaderView
{
QStringList heads;

public:
    QHeaderViewR(QStringList _heads):QHeaderView(Qt::Horizontal)
    {

        heads = _heads;
    }

    void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    {


        QPen pen(Qt::black);
        painter->setPen(pen);

        painter->rotate(90);
        painter->translate(0,-rect.width()+1);

        QRect copy = rect;

        copy.setWidth(rect.height());
        copy.setHeight(rect.width());
        copy.moveTo(0,-this->sectionPosition(logicalIndex));

        if (logicalIndex == 0)
        {
            copy.setHeight(rect.width()-1);
        }

        painter->drawText(copy, " " + heads.at(logicalIndex));
        painter->drawRect(copy);
    }
};

答案 1 :(得分:0)

由于QHeaderView只是一个视图,您应该从模型中获取数据。

所以,类似于base implementation in QHeaderView

QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels)

要在模型上设置标题,请使用例如

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:webflow-config="http://www.springframework.org/schema/webflow-config"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
     http://www.springframework.org/schema/webflow-config
 http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.4.xsd">

    <context:component-scan base-package="com.rog.webshop.controller"/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <!--<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/>-->
    <mvc:annotation-driven conversion-service="conversionService" enable-matrix-variables="true" validator="validator"/>


    <!--TILES -->
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

    <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.tiles3.TilesView">
        </property>
        <property name="order" value="-2"/>
    </bean>







    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

        <property name="converters">
            <list>
                <bean id="roleToUserProfile" class="com.rog.webshop.util.RoleToUserProfileConverter"/>
                <bean id="categoryToProduct" class="com.rog.webshop.util.CategoryConverter"/>
            </list>
        </property>
    </bean>


    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10240000"/>
    </bean>






    <!--WEBFLOW CONFIG-->
    <webflow-config:flow-executor id="flowExecutor"
                                  flow-registry="flowRegistry"/>
    <webflow-config:flow-builder-services id="flowBuilderServices"
                                          validator="validator"
                                          view-factory-creator="mvcViewFactoryCreator"/>
    <webflow-config:flow-registry id="flowRegistry"
                                  base-path="WEB-INF/flows"
                                  flow-builder-services="flowBuilderServices">
        <webflow-config:flow-location path="/checkout/checkout-flow.xml"
                                      id="checkout"/>
    </webflow-config:flow-registry>

    <!-- Configures Web Flow to use Tiles to create views for rendering; Tiles allows for applying consistent layouts to your views -->
    <bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
        <property name="viewResolvers" ref="tilesViewResolver" />
    </bean>
    <bean id="flowHandlerMapping" class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="flowRegistry" ref="flowRegistry"/>
        <property name="order" value="-1"/>
    </bean>
    <bean id="flowHandlerAdapter" class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor"/>
    </bean>







    <!-- Configure to plugin JSON as request and response in method handler -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter"/>
            </list>
        </property>
    </bean>
    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </bean>


</beans>