C ++ GUI QSpinBox没有出现

时间:2014-01-19 00:01:18

标签: c++ qt

我的程序正确编译并显示我的按钮但由于某种原因它不显示QSpinBox。我是C ++ qt GUI的新手,所以任何输入都会非常感激。我还检查了旋转器是否被按钮重叠,但它们没有。

//
// mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>

namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void clearx();
    void equalsx();
    void addx();
    void subtractx();
    void multiplyx();
    void dividex();
    void firstnumberx();
    void secondnumberx();
private:
    QLabel *label;
    QPushButton *equal;
    QPushButton *clear;
    QPushButton *equals;
    QPushButton *add;
    QPushButton *subtract;
    QPushButton *multiply;
    QPushButton *divide;
    QSpinBox *spinner;
    QSpinBox *spinner2;
};

#endif // MAINWINDOW_H

//
// mainwindow.cpp

#include "mainwindow.h"
#include <QTCore/QCoreApplication>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)

{
    label = new QLabel("0,this");
    label -> setGeometry(QRect(QPoint(75,75),QSize(50,200)));

    clear = new QPushButton("Clear", this);
    clear  -> setGeometry(QRect(QPoint(80,300),QSize(50,50)));
    connect(clear,SIGNAL(released()),this,SLOT(clearx()));

    equal = new QPushButton("Equal", this);
    equal  -> setGeometry(QRect(QPoint(110,300),QSize(50,50)));
    connect(equal,SIGNAL(released()),this,SLOT(equalx()));

    add = new QPushButton("Add", this);
    add -> setGeometry(QRect(QPoint(140,300),QSize(50,50)));
    connect(add,SIGNAL(released()),this,SLOT(addx()));

    subtract = new QPushButton("Subtract", this);
    subtract -> setGeometry(QRect(QPoint(170,300),QSize(50,50)));
    connect(subtract,SIGNAL(released()),this,SLOT(subtractx()));

    multiply = new QPushButton("Multiply", this);
    multiply -> setGeometry(QRect(QPoint(200,300),QSize(50,50)));
    connect(multiply,SIGNAL(released()),this,SLOT(multiplyx()));

    divide = new QPushButton("Divide", this);
    divide -> setGeometry(QRect(QPoint(230,300),QSize(50,50)));
    connect(divide,SIGNAL(released()),this,SLOT(dividex()));

    spinner = new QSpinBox;
    spinner -> setGeometry(QRect(QPoint(130,150),QSize(50,50)));
    connect(divide,SIGNAL(released()),this,SLOT(firstnumberx()));
    spinner->setRange(1,10);

    spinner2 = new QSpinBox;
    spinner2 -> setGeometry(QRect(QPoint(190,150),QSize(50,50)));
    connect(divide,SIGNAL(released()),this,SLOT(secondnumberx()));
    spinner2->setRange(1,10);
}


void MainWindow::clearx() {}
void MainWindow::equalsx() {}
void MainWindow::addx() {}
void MainWindow::subtractx() {}
void MainWindow::multiplyx() {}
void MainWindow::dividex() {}
void MainWindow::firstnumberx() {}
void MainWindow::secondnumberx() {}
MainWindow::~MainWindow() {}

2 个答案:

答案 0 :(得分:1)

为了使窗口小部件显示在另一个窗口小部件中,您需要设置父子关系。最简单的方法是将指向父项的构造函数的父指针作为参数传递。

对于QSpinBox个对象,这样做是这样的:

spinner = new QSpinBox(this);
spinner2 = new QSpinBox(this);

您的其余代码中充满了硬编码的几何图形。对于小型GUI,这可能不是一个问题,但对于较大的GUI来说可能会变得有点噩梦。您是否考虑过使用QtDesigner来设计GUI?您可能还会发现layout management classes有助于您的GUI设计更好地维护位置和调整子小部件的大小。

答案 1 :(得分:0)

添加this作为QSpinBox构造函数调用的参数。