当坐标>时,QGraphicsProxyWidget :: setPos(qreal x,qreal y)无法正确放置在QGraphicsScene中。 2 ^ 15

时间:2014-04-28 13:10:41

标签: qt position qgraphicsscene

我有一个QGraphicsScene大尺寸用于显示数据库内容。

由于QGraphicsScene的方法setPos(),我放置在QGraphicsPixmapItem中的图片的一部分由数千张照片组成。

在这些图片的前面,我放置了最终可通过QCheckbox访问的QGraphicsProxyWidget个。但是QGraphicsProxyWidget::setPos(qreal x, qreal y)导致在QGraphicsScene中以有符号的短整数形式提供坐标 但是,执行QGraphicsProxyWidget::pos()会正确返回原始坐标,甚至高于2 ^ 16。

以下是代码:

QCheckBox* checkbox = new QCheckBox("", this);

QWidget* dummyWidget = new QWidget; //used for having a transparent background
dummyWidget->setStyleSheet("background-color:transparent;"
                           "outline-color:transparent;"
                           "font-size: 8pt;");

QHBoxLayout* dummyLayout = new QHBoxLayout(dummyWidget);
dummyLayout->addWidget(checkbox);

QGraphicsProxyWidget* proxyWidget = scene.addWidget(dummyWidget);
proxyWidget->setPos(0, 120*i);

当120 * i介于32769和65536之间时,QChekBox不会显示。对于上述值,QCheckBox es显示为y = value - 65536

我尝试了许多没有成功的事情,比如 - proxyWidget->moveBy
- dummyWidget->move
- dummyWidget->setFixedSize(0, 240*i); checkbox->move(0, 120*i);

任何解决方案?

PS:我所依赖的工具链/交叉工具链嵌入了QT4.8.1。对于桌面端。
我没办法改变它,所以升级到QT5.x不是一个选择。

1 个答案:

答案 0 :(得分:0)

你可以使用下一个技巧:

 // Function that define the mandatory product category
         function your_mandatory_category_slug(){
        // DEFINE HERE the SLUG of the needed product category
            $category = 'cxsuite-download-option';
            return $category;
         }

         function your_mandatory_category_slug_h(){

             // DEFINE HERE the SLUG of the needed product category
            $category_h = 'cxsuite-hosted-option';
            return $category_h;
         }
    // Conditional function that returns true if the mandatory product category is in cart
    function has_mandatory_category(){
        $category_needed = your_mandatory_category_slug();
       $category_needed_h = your_mandatory_category_slug_h();
        $has_cat = false;

        // Iterrating each item in cart and detecting…
        foreach ( WC()->cart->get_cart() as $item ) {

            // Detects if the needed product category is in cart items
            if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {
                $has_cat = true;
                break;
            }
            elseif ( has_term($category_needed_h, 'product_cat', $item['product_id'] ) ) {
                $has_cat = true;
                break;
            }
        }
        return $has_cat;
     }
// Function that display a message if there is not in cart a mandatory product category
function mandatory_category_display_message() {
        $category_needed = your_mandatory_category_slug();
       $category_needed_h = your_mandatory_category_slug_h();


    // check that cart is not empty (for cart and product category archives)
    if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( $category_needed ) || is_product_category( $category_needed_h ) ) ){
        $category_needed_single=null;
        if( $category_needed=='cxsuite-download-option' ){
            $category_needed_single='cxsuite-download-option';
        }{
            $category_needed_single=$category_needed_h;
        }
        $category_obj = get_term_by( 'slug', $category_needed_single, 'product_cat' );

        if ( is_wp_error( $category_obj ) ) return;


        // Display message when product category is not in cart items
        if ( !has_mandatory_category() ) {
            $category_name = $category_obj->name;
            $category_url = get_term_link( $category_needed_single, 'product_cat' );

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( __( '<strong>Reminder:</strong> You have chosen Addon that can only be purchased together with a %1$s. Please add a %1$s (full or trial) before checking out. Please return <a href="%2$s"> here to "%1$s" product page</a>', 'your_theme_domain'), $category_name, $category_url ), 'error' );

        }

    }
}
add_action( 'woocommerce_before_main_content', 'mandatory_category_display_message', 30 ); // for product mandatory category archives pages
add_action( 'woocommerce_check_cart_items', 'mandatory_category_display_message' ); // for cat page


// Function that redirect from checkout to mandatory product category archives pages
function mandatory_category_checkout_redirect() {

    // If cart is not empty on checkout page
    if( !WC()->cart->is_empty() && is_checkout() ){
        $category_needed = your_mandatory_category_slug();
        $category_needed_h = your_mandatory_category_slug_h();
        $category_needed_single=null;
        if(is_product_category( $category_needed )){
            $category_needed_single=$category_needed;
        }else{
            $category_needed_single=$category_needed_h;
        }

        // If missing product category => redirect to the products category page
        if ( !has_mandatory_category() )
            wp_redirect( get_term_link( $category_needed_single, 'product_cat' ) );
    }

}
add_action('template_redirect', 'mandatory_category_checkout_redirect');

现在,您可以调用此函数:

void setNewPos(QGraphicsItem *item, QPointF pos)
{
    item->resetTransform();
    QTransform trans = item->transform();
    item->setTransform(trans.translate(pos.x(), pos.y()));
}