QML 输入框的一些问题



  • 0_1523974188470_c1c7dd85-f326-4806-9c58-002d167c83b4-undefined

    如图所示,QML所有输入控件都会有的一个问题,想屏蔽系统自带的大光标样式,而采用自定义的光标样式
    0_1523974286223_149e8668-8073-4842-adc2-d08808a3acc3-undefined



  • @lowbees 试一试:

    SpinBox
    {
        contentItem.selectByMouse: false
    }
    


  • @jiangcaiyang 这个属性默认就是false的



  • 是不是可以activeFocusOnPress:false或者selectByMouse: false。selectByMouse默认是true



  • 我记得是可以自定义光标样式的



  • @linbin823 @青山白云 真正想要的是能自定义这个鼠标(pc的说法)样式, 0_1524020326995_676b9a70-65a0-4b36-84f4-e4b2ae2f996e-undefined 类似于这种效果



  • @lowbees 我记得哪里看到过源代码,可以进行处理的。稍等一下,我查找一下。



  • 那我猜你不能直接用controls这种现成的控件了,得深度定制。



  • @linbin823 具体在这里:
    G:\qt-everywhere-src-5.10.0\qtbase\src\android\jar\src\org\qtproject\qt5\android
    可能的方式就是

    1、先读懂代码的内容
    2、尝试用Java编译这部分的jar包
    3、再替换掉你编译的Qt程序中的相应部分

    顺便看看是否有机会可以通过继承的方式替换。你再看一看。
    附:CursorHandle.java文件

    /****************************************************************************
    **
    ** Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
    ** Contact: http://www.qt.io/licensing/
    **
    ** This file is part of the Android port of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:LGPL$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see https://www.qt.io/terms-conditions. For further
    ** information use the contact form at https://www.qt.io/contact-us.
    **
    ** GNU Lesser General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU Lesser
    ** General Public License version 3 as published by the Free Software
    ** Foundation and appearing in the file LICENSE.LGPL3 included in the
    ** packaging of this file. Please review the following information to
    ** ensure the GNU Lesser General Public License version 3 requirements
    ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
    **
    ** GNU General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU
    ** General Public License version 2.0 or (at your option) the GNU General
    ** Public license version 3 or any later version approved by the KDE Free
    ** Qt Foundation. The licenses are as published by the Free Software
    ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
    ** included in the packaging of this file. Please review the following
    ** information to ensure the GNU General Public License requirements will
    ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
    ** https://www.gnu.org/licenses/gpl-3.0.html.
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    
    package org.qtproject.qt5.android;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.ImageView;
    import android.content.res.TypedArray;
    import android.graphics.drawable.Drawable;
    import android.view.MotionEvent;
    import android.widget.PopupWindow;
    import android.app.Activity;
    import android.view.ViewTreeObserver;
    
    /* This view represents one of the handle (selection or cursor handle) */
    class CursorView extends ImageView
    {
        private CursorHandle mHandle;
        // The coordinare which where clicked
        private int m_offsetX;
        private int m_offsetY;
    
        CursorView (Context context, CursorHandle handle) {
            super(context);
            mHandle = handle;
        }
    
        // Called when the handle was moved programatically , with the delta amount in pixels
        public void adjusted(int dx, int dy) {
            m_offsetX += dx;
            m_offsetY += dy;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getActionMasked()) {
            case MotionEvent.ACTION_DOWN: {
                m_offsetX = Math.round(ev.getRawX());
                m_offsetY = Math.round(ev.getRawY());
                break;
            }
    
            case MotionEvent.ACTION_MOVE: {
                mHandle.updatePosition(Math.round(ev.getRawX()) - m_offsetX,
                                        Math.round(ev.getRawY()) - m_offsetY);
                break;
            }
    
            case MotionEvent.ACTION_UP:
                break;
    
            case MotionEvent.ACTION_CANCEL:
                break;
            }
            return true;
        }
    
    }
    
    // Helper class that manages a cursor or selection handle
    public class CursorHandle implements ViewTreeObserver.OnPreDrawListener
    {
        private View m_layout = null;
        private CursorView m_cursorView = null;
        private PopupWindow m_popup = null;
        private int m_id;
        private int m_attr;
        private Activity m_activity;
        private int m_posX = 0;
        private int m_posY = 0;
        private int m_lastX;
        private int m_lastY;
        int tolerance;
        private boolean m_rtl;
    
        public CursorHandle(Activity activity, View layout, int id, int attr, boolean rtl) {
            m_activity = activity;
            m_id = id;
            m_attr = attr;
            m_layout = layout;
            DisplayMetrics metrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            tolerance = Math.round(2 * metrics.density);
            m_lastX = m_lastY = -1 - tolerance;
            m_rtl = rtl;
        }
    
        private boolean initOverlay(){
            if (m_popup == null){
    
                Context context = m_layout.getContext();
                int[] attrs = {m_attr};
                TypedArray a = context.getTheme().obtainStyledAttributes(attrs);
                Drawable drawable = a.getDrawable(0);
    
                m_cursorView = new CursorView(context, this);
                m_cursorView.setImageDrawable(drawable);
                // m_layout.addView(m_cursorView);
    
                m_popup = new PopupWindow(context, null, android.R.attr.textSelectHandleWindowStyle);
                m_popup.setSplitTouchEnabled(true);
                m_popup.setClippingEnabled(false);
                m_popup.setContentView(m_cursorView);
                m_popup.setWidth(drawable.getIntrinsicWidth());
                m_popup.setHeight(drawable.getIntrinsicHeight());
    
                m_layout.getViewTreeObserver().addOnPreDrawListener(this);
            }
            return true;
        }
    
        // Show the handle at a given position (or move it if it is already shown)
        public void setPosition(final int x, final int y){
            initOverlay();
    
            final int[] location = new int[2];
            m_layout.getLocationOnScreen(location);
    
            int x2 = x + location[0];
            int y2 = y + location[1];
    
            if (m_id == QtNative.IdCursorHandle) {
                x2 -= m_cursorView.getWidth() / 2 ;
            } else if ((m_id == QtNative.IdLeftHandle && !m_rtl) || (m_id == QtNative.IdRightHandle && m_rtl)) {
                x2 -= m_cursorView.getWidth() * 3 / 4;
            } else {
                x2 -= m_cursorView.getWidth() / 4;
            }
    
            if (m_popup.isShowing()) {
                m_popup.update(x2, y2, -1, -1);
                m_cursorView.adjusted(x - m_posX, y - m_posY);
            } else {
                m_popup.showAtLocation(m_layout, 0, x2, y2);
            }
    
            m_posX = x;
            m_posY = y;
        }
    
        public void hide() {
            if (m_popup != null) {
                m_popup.dismiss();
            }
        }
    
        // The handle was dragged by a given relative position
        public void updatePosition(int x, int y) {
            if (Math.abs(m_lastX - x) > tolerance || Math.abs(m_lastY - y) > tolerance) {
                QtNative.handleLocationChanged(m_id, x + m_posX, y + m_posY);
                m_lastX = x;
                m_lastY = y;
            }
        }
    
        @Override
        public boolean onPreDraw() {
            // This hook is called when the view location is changed
            // For example if the keyboard appears.
            // Adjust the position of the handle accordingly
            if (m_popup != null && m_popup.isShowing())
                setPosition(m_posX, m_posY);
    
            return true;
        }
    }
    


  • @jiangcaiyangQML 输入框的一些问题 中说:

    is part of the Android port of the Qt Toolkit.

    这么说是要重新编译qt?





  • @lowbees 不清楚,我只是看到有这样的源码了,具体怎么玩转,希望你再看一下,是否可以做到不修改源代码即可个性化输入光标。



  • @jiangcaiyang 😆 还是得等官方或者大佬解决了



  • 0_1524313514973_1b13449a-d1bf-4398-80ae-1fc61bcb5698-undefined 突然在官方给出showroom看到一个东西 https://showroom.qt.io/qml-creator/



  • 找到了github的地址 https://github.com/olegyadrov/qmlcreator
    ,具体位置在这里:qmlcreator/qml/components/CCodeArea.qml



  • @lowbees 赶紧看一看它的精髓,估计就是什么设置导致可以使用默认的选择光标样式。


 

最近的回复

  • 大家好!最近应该很好吧。世界杯来了,有没有喝一杯呢?
    我很高兴地告诉大家,我们在上周末的时候完成了Live2D的改造,将我们的看板娘换了哦。原先是蕾姆。
    0_1529471654453_dfe4b40e-ad64-4b7a-9d58-5c48609ea3e2-image.png
    同时我对样式也做了一些微调,比如说这里:
    0_1529471386270_828e7fbb-fe0c-4f6b-bb52-03938ac7e19f-image.png
    看起来稍微好了一些。

    随后论坛进行升级了哦,升级到了最新1.9.2版本了,各种功能应该得到优化了吧。
    最后欢迎大家继续来玩~

    阅读更多
  • Qt for Python终于发布了!
    以前的我,可能接触了一点Python,但是都没有坚持下来,现在Python水平还很弱呢。不过我基本语法还是了解一点的。所以我打算借着Qt for Python的东风,来尝试一下Python的开发。

    1、去Python 官网安装Python。地址是:

    https://www.python.org/downloads/mac-osx
    0_1529422911935_1.png
    我下载的版本是3.6.5

    2、双击pkg安装包进行安装,按照提示安装即可,不必做什么设置。
    0_1529422932496_2.png
    3、安装pip。pip是一个类似npm的快速安装器,非常适合python安装。由于Mac的安全策略,这个时候要输入的命令以sudo开始:

    sudo python get-pip.py

    4、需要安装Qt官网提供的python安装包。Qt的Python安装包并没有放入Pypi的地址,因为Qt库本身就偏大。
    但是依然能够通过pip安装。只是安装的命令稍微长:

    sudo pip install --index-url=https://download.qt.io/official_releases/QtForPython/ pyside2

    然后输出的结果是:
    daxiongtekiMacBook-Air:~ jiangcaiyang$ sudo pip install --index-url=https://download.qt.io/official_releases/QtForPython/ pyside2
    Password:
    The directory '/Users/jiangcaiyang/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
    The directory '/Users/jiangcaiyang/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
    Looking in indexes: https://download.qt.io/official_releases/QtForPython/
    Collecting pyside2
    Downloading https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-5.11.0-5.11.0-cp27-cp27m-macosx_10_11_intel.whl (125.7MB)
    100% |████████████████████████████████| 125.7MB 1.0MB/s
    matplotlib 1.3.1 requires nose, which is not installed.
    matplotlib 1.3.1 requires tornado, which is not installed.
    Installing collected packages: pyside2
    Successfully installed pyside2-5.11.0
    安装成功。

    5、安装成功了,开始写一个测试的程序吧!
    最简单的,当然是Hello Qt for Python啦。很简单,使用Qt Creator,很方便地可以创建这样的Python脚本,使用方法也和C++版本的Qt应用相同:

    #!/usr/bin/env python # -*- coding: utf-8 -*- from PySide2.QtWidgets import QApplication, QLabel app = QApplication( [ ] ) label = QLabel( "您好 Qt for Python!" ) label.show( ) app.exec_( )

    0_1529422962008_4.jpg
    其中第一行表示用/usr/bin/python 来解析,大家可以在命令提示符输入“which python”来看结果:
    第二行则是在Python 3.x下不用写了,设定的是utf-8编码格式。大家最好也是使用这样的格式,因为这样遇到编码的坑最少。
    因为Python没有main()函数的说法,所以直接从上往下运行就可以了。Python是弱语言,所以不用声明类型,这里还有一点不一样,就是
    app.exec_( ),和C++版本不同的是,添加上了一个下划线。可能和Python内置的函数冲突了?我还需要再调查一下。

    接下来我就仿照例子写了一个更加复杂的Python程序,大致是这样的:

    #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import random from PySide2.QtCore import Qt from PySide2.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QVBoxLayout ) class MyWidget( QWidget ): def __init__( self ): QWidget.__init__( self ) self.hello = [ "Hallo welt!", "Ciao mondo", "Hei maailma", "Hola mundo", "Hei verden!" ] self.button = QPushButton( "Click me!" ) self.text = QLabel( "Hello World" ) self.text.setAlignment( Qt.AlignCenter ) self.layout = QVBoxLayout( ) self.layout.addWidget( self.text ) self.layout.addWidget( self.button ) self.setLayout( self.layout ) self.button.clicked.connect( self.magic ) def magic( self ): self.text.setText( random.choice( self.hello ) ) if __name__ == "__main__": app = QApplication( sys.argv ) widget = MyWidget( ) widget.resize( 640, 480 ) widget.show( ) sys.exit( app.exec_( ) )

    程序运行起来还行吧!
    0_1529422988117_5.png

    阅读更多
  • @jiangcaiyang 如果需要自动化测试,也用的上

    阅读更多
  • @青山白云 这个可能有点用,但是可能更多局限于爬虫领域吧。

    阅读更多

关注我们

微博
QQ群











召唤伊斯特瓦尔