Problem with spinbox?



  • I am using three spinbox in my program and the problem is that while running the program the cursor always remain in one of the spinbox but i want that in initial condition the cursor position should not be in the spin box.



  • Does it giving the focus to another component that is not the spin boxes solve the problem?



  • actually other widget are not getting keyboard focus . While pressing tab the focus going to next spinbox.



  • Not sure why this behavior is wrong for you. Could you please post an example to help us understand the problem?



  • I have one code where it is working fine but after some modification in the same code it is not working. can you give me your mail id so that i can post you the code.



  • Please keep the discussion on the -list- forum. That way, we all can learn from it.



  • but i can't post the code here because it is too long to accommodate here



  • Please cut it down to a postable example showing the problem then. You will need to do that anyway; you can not expect another to dig through hundreds of lines of irrelevant code to try to help you find your problem. Debugging is your task, not ours.



  • This is the code
    @main.cpp
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;

    int min = 0;
    int max = 100;
    QPixmap pixmap(":/simple_track_bar.png");
    QPixmap pixmap1(":/simple_track_thumb.png");
    QRect rect(50,60,4,118);
    
    MBSlider *mb3 = new MBSlider(min, max, pixmap, pixmap1,rect,false,&w);
    QSpinBox *spinBox3 = new QSpinBox;
    spinBox3->setRange(0, 100);
    
    QObject::connect(spinBox3, SIGNAL(valueChanged(int)),mb3, SLOT(setValue2(int)));
    QObject::connect(mb3, SIGNAL(sliderMoved(int)),spinBox3, SLOT(setValue(int)));
    

    @

    @
    mbslider.cpp

    MBSlider::MBSlider(int min,
    int max,
    QPixmap image,
    QPixmap image1,
    QRect rect1,
    bool mode,
    QWidget *parent) :
    QAbstractSlider(parent)
    {
    rect = rect1;

    setRange (min, max);
    valuePos = minimum ();
    valuePos_vertical = minimum ();
    pixmap  = image;
    pixmap1 = image1;
    QColor color(Qt::white);
    pixmap.setMask (pixmap.createMaskFromColor (color, Qt::MaskInColor));
    this->setGeometry (rect);
    slider_mode = mode;
    if (slider_mode) {
        draw_horizontal_slider ();
    } else {
        draw_vertical_slider ();
    }
    this->repaint ();
    

    }

    MBSlider::~MBSlider()

    {

    }
    void MBSlider::draw_vertical_slider ()
    {
    this->setOrientation (Qt::Vertical);
    if (pixmap.width () > pixmap1.width ()) {
    mid_pos = ((pixmap.width () >> 1) - (pixmap1.width ()>> 1));
    this->setFixedSize (pixmap.width (), rect.height ());
    } else {
    mid_pos = ((pixmap1.width () >> 1) - (pixmap.width ()>>1));
    this->setFixedSize (pixmap1.width (), rect.height ());
    }

    pos_width = (rect.height () - (pixmap1.height ()));
    newPos = pos_width;
    

    }
    void MBSlider::draw_horizontal_slider ()
    {
    newPos = 0;
    this->setOrientation(Qt::Horizontal);
    if (pixmap1.height () > pixmap.height ()) {
    this->setFixedSize (rect.width (), pixmap1.height ());
    mid_pos = ((pixmap1.height() >> 1) - (pixmap.height () >> 1));
    } else {
    this->setFixedSize (rect.width (), pixmap.height ());
    mid_pos = ((pixmap.height () >> 1) - (pixmap1.height () >> 1));
    }

    pos_width = (rect.width () - (pixmap1.width ()));
    

    }
    void MBSlider::mousePressEvent ( QMouseEvent * event )

    {
    if (event->button() == Qt::LeftButton) {

        if (slider_mode) {
    
            newPos = event->x ();
        } else {
            newPos = event->y ();
        }
        calcNewValue();
    
        if (slider_mode) {
            emit sliderMoved(valuePos);
        } else {
            emit sliderMoved (valuePos_vertical);
        }
    
        repaint();
    }
    QAbstractSlider::mousePressEvent (event);
    

    }
    void MBSlider::keyPressEvent (QKeyEvent *event)
    {
    qDebug ()<<"key pressed";
    // Check whether the key entered is a enter key or not.
    if (slider_mode) {
    // Check for horizontal slider.
    if (event->key () == Qt::Key_Right ) {
    event->accept ();
    newPos = newPos + 1;
    calcNewValue ();
    emit sliderMoved (valuePos);
    this->repaint ();
    } else if (event->key () == Qt::Key_Left) {
    event->accept ();
    newPos = newPos - 1;
    calcNewValue ();
    emit sliderMoved (valuePos);
    this->repaint ();
    }
    } else {
    if (event->key () == Qt::Key_Up) {
    event->accept ();
    newPos = newPos - 1;
    calcNewValue ();
    emit sliderMoved (valuePos);
    this->repaint ();
    } else if (event->key () == Qt::Key_Down){
    event->accept ();
    newPos = newPos + 1;
    calcNewValue ();
    emit sliderMoved (valuePos);
    this->repaint ();
    }
    }
    }

    void MBSlider::mouseMoveEvent ( QMouseEvent * event )

    {
    if (event->buttons() & Qt::LeftButton) {
    is_down = true;
    if (orientation() == Qt::Horizontal) {
    newPos = event->x();
    } else {
    newPos = event->y ();
    }
    calcNewValue();
    if (slider_mode) {
    emit sliderMoved(valuePos);
    } else {
    emit sliderMoved (valuePos_vertical);
    }
    }
    repaint();
    QAbstractSlider::mouseMoveEvent (event);
    }

    void MBSlider::paintEvent ( QPaintEvent * event )

    {
    QPainter painter(this);
    QRectF center;
    int draw_pos;
    int new_pos;

    if (slider_mode) {
    
        // Draw horizontal slider.
        draw_pos = newPos + (pixmap1.width () >> 1);
    
        ..................................................................................
    

    }

    int MBSlider::getValue()

    {
    int value_pos;

    if (slider_mode) {
        value_pos = valuePos;
    } else {
        value_pos = valuePos_vertical;
    }
    return value_pos;
    

    }

    void MBSlider::calcNewValue()

    {
    if (newPos > pos_width) {
    newPos = pos_width;
    } else if (newPos < 0) {
    newPos = 0;
    }
    qDebug ()<<valuePos<<"valuepos";
    qDebug ()<<newPos<< "new POs";
    valuePos = (((float (maximum () - minimum ()) ) / pos_width) * newPos) + minimum ();

    if (!slider_mode) {
        valuePos_vertical = minimum () + (maximum () - valuePos);
    
    }
    

    }

    void MBSlider::setValue2(int v)
    {
    // Work for all range with all pixels.
    //calcNewValue();

    if (slider_mode) {
        valuePos = v;
        emit valueChanged (valuePos);
        emit sliderMoved (valuePos);
        newPos = (float(valuePos - this->minimum ()) / (this->maximum() - this->minimum ()) * pos_width);
        this->repaint();
    
    } else {
        valuePos = minimum() + (maximum () - v);
        newPos = (float(valuePos - this->minimum ())/(this->maximum () - this->minimum ()) * pos_width);
        emit valueChanged (v);
        emit sliderMoved (v);
        this->repaint ();
    }
    

    }
    @



  • Not sure I'm getting the problem, but does not
    @mb3->setFocus();@

    solve it? You are asking, if I get it right, that the spin box does not have the focus initially.
    Moreover, the problem you are trying to face here is the cause of "another problem you posted":http://developer.qt.nokia.com/forums/viewthread/11629/. You should try to solve one per time (in my opinion).



  • ya i will try that


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.