Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with spinbox?

Problem with spinbox?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 6.7k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    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.

    Pratik Agrawal

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pratik041
        wrote on last edited by
        #3

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

        Pratik Agrawal

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pratik041
            wrote on last edited by
            #5

            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.

            Pratik Agrawal

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pratik041
                wrote on last edited by
                #7

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

                Pratik Agrawal

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pratik041
                    wrote on last edited by
                    #9

                    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 ();
                    }
                    

                    }
                    @

                    Pratik Agrawal

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fluca1978
                      wrote on last edited by
                      #10

                      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).

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pratik041
                        wrote on last edited by
                        #11

                        ya i will try that

                        Pratik Agrawal

                        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