在 QPushButton 上调用另一个小部件应用程序

Call another widget application on QPushButton

我想在按钮上调用另一个小部件应用程序计算器。直到这次我获得了成功,但它与父小部件重叠。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>

class QLineEdit;
class QComboBox;
class calculator;


namespace Ui {
class MainWindow;
}

class MainWindow : public QWidget
{
    Q_OBJECT

public:MainWindow();

public slots:

       void calcButtonPressed();

private:
               calculator *calc;

};

#endif // MAINWINDOW_H

和mainwindow.cpp

MainWindow::MainWindow(){
....
 QPushButton *calcButton = new QPushButton(tr("Calc"));
    connect(calcButton,SIGNAL(clicked(bool)),this,SLOT(calcButtonPressed()));
....
}

void MainWindow::calcButtonPressed(){

     calc = new calculator(this);
    calc->show();

}

calculator.cpp

#include<QtWidgets>
#include<cmath>
#include "button.h"
#include "calculator.h"





calculator::calculator(QWidget *parent): QWidget(parent)
{
    sumInMemory=0.0;
    sumSoFar=0.0;
    factorSoFar=0.0;
    waitingForOperand=true;

    display=new QLineEdit("0");
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);

QFont font=display->font();
font.setPointSize(font.pointSize()+8);
display->setFont(font);

for (int i = 0; i < NumDigitButton; ++i) {
        digitButton[i] = createButton(QString::number(i), SLOT(digitClicked()));
    }

button *pointButton =createButton(tr("."),SLOT(pointClicked()));
button *changeSignButton=createButton(tr("1"),SLOT(changeSignClicked()));

button *backspaceButton=createButton(tr("Backspace"),SLOT(backspaceClicked()));
button *clearButton=createButton(tr("Clear"),SLOT(clear()));
button *clearAllButton=createButton(tr("Clear All"),SLOT(clearAll));

button *clearMemoryButton=createButton(tr("MC"),SLOT(clearMemory()));
button *readMemoryButton=createButton(tr("MR"),SLOT(readMemory()));
button *setMemoryButton=createButton(tr("MS"),SLOT(setMemory()));
button *addToMemoryButton=createButton(tr("M+"),SLOT(addToMemory()));

button *divisionButton = createButton(tr("37"), SLOT(multiplicativeOperatorClicked()));
button *timesButton = createButton(tr("37"), SLOT(multiplicativeOperatorClicked()));
 button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked()));
 button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));

 button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked()));
 button *powerButton = createButton(tr("x22"), SLOT(unaryOperatorClicked()));
 button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked()));
 button *equalButton = createButton(tr("="), SLOT(equalClicked()));

 QGridLayout *mainLayout = new QGridLayout;
     mainLayout->setSizeConstraint(QLayout::SetFixedSize);
     mainLayout->addWidget(display, 0, 0, 1, 6);
     mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
     mainLayout->addWidget(clearButton, 1, 2, 1, 2);
     mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);

     mainLayout->addWidget(clearMemoryButton, 2, 0);
     mainLayout->addWidget(readMemoryButton, 3, 0);
     mainLayout->addWidget(setMemoryButton, 4, 0);
     mainLayout->addWidget(addToMemoryButton, 5, 0);

     for (int i = 1; i < NumDigitButton; ++i) {
         int row = ((9 - i) / 3) + 2;
         int column = ((i - 1) % 3) + 1;
         mainLayout->addWidget(digitButton[i], row, column);
     }

     mainLayout->addWidget(digitButton[0], 5, 1);
     mainLayout->addWidget(pointButton, 5, 2);
     mainLayout->addWidget(changeSignButton, 5, 3);

     mainLayout->addWidget(divisionButton, 2, 4);
     mainLayout->addWidget(timesButton, 3, 4);
     mainLayout->addWidget(minusButton, 4, 4);
     mainLayout->addWidget(plusButton, 5, 4);

     mainLayout->addWidget(squareRootButton, 2, 5);
     mainLayout->addWidget(powerButton, 3, 5);
     mainLayout->addWidget(reciprocalButton, 4, 5);
     mainLayout->addWidget(equalButton, 5, 5);
     setLayout(mainLayout);

     setWindowTitle(tr("calculator"));
 }
void calculator::digitClicked()
{
    button *clickedButton = qobject_cast<button *>(sender());
    int digitValue = clickedButton->text().toInt();
    if (display->text() == "0" && digitValue == 0.0)
        return;

    if (waitingForOperand) {
        display->clear();
        waitingForOperand = false;
    }
    display->setText(display->text() + QString::number(digitValue));
}

void calculator::unaryOperatorClicked()
{
    button *clickedButton = qobject_cast<button *>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();
    double result = 0.0;

    if (clickedOperator == tr("Sqrt")) {
        if (operand < 0.0) {
            abortOperation();
            return;
        }
        result = std::sqrt(operand);
    } else if (clickedOperator == tr("x22")) {
        result = std::pow(operand, 2.0);
    } else if (clickedOperator == tr("1/x")) {
        if (operand == 0.0) {
            abortOperation();
            return;
        }
        result = 1.0 / operand;
    }
    display->setText(QString::number(result));
    waitingForOperand = true;
}

void calculator::additiveOperatorClicked()
{
    button *clickedButton = qobject_cast<button *>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();

    if (!pendingMultiplicativeOperator.isEmpty()) {
        if (!calculate(operand, pendingMultiplicativeOperator)) {
            abortOperation();
            return;
        }
        display->setText(QString::number(factorSoFar));
        operand = factorSoFar;
        factorSoFar = 0.0;
        pendingMultiplicativeOperator.clear();
    }

    if (!pendingAdditiveOperator.isEmpty()) {
        if (!calculate(operand, pendingAdditiveOperator)) {
            abortOperation();
            return;
        }
        display->setText(QString::number(sumSoFar));
    } else {
        sumSoFar = operand;
    }

    pendingAdditiveOperator = clickedOperator;
    waitingForOperand = true;
}

void calculator::multiplicativeOperatorClicked()
{
    button *clickedButton = qobject_cast<button *>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();

    if (!pendingMultiplicativeOperator.isEmpty()) {
        if (!calculate(operand, pendingMultiplicativeOperator)) {
            abortOperation();
            return;
        }
        display->setText(QString::number(factorSoFar));
    } else {
        factorSoFar = operand;
    }

    pendingMultiplicativeOperator = clickedOperator;
    waitingForOperand = true;
}

void calculator::equalClicked()
{
    double operand = display->text().toDouble();

    if (!pendingMultiplicativeOperator.isEmpty()) {
        if (!calculate(operand, pendingMultiplicativeOperator)) {
            abortOperation();
            return;
        }
        operand = factorSoFar;
        factorSoFar = 0.0;
        pendingMultiplicativeOperator.clear();
    }
    if (!pendingAdditiveOperator.isEmpty()) {
        if (!calculate(operand, pendingAdditiveOperator)) {
            abortOperation();
            return;
        }
        pendingAdditiveOperator.clear();
    } else {
        sumSoFar = operand;
    }

    display->setText(QString::number(sumSoFar));
    sumSoFar = 0.0;
    waitingForOperand = true;
}

void calculator::pointClicked()
{
    if (waitingForOperand)
        display->setText("0");
    if (!display->text().contains('.'))
        display->setText(display->text() + tr("."));
    waitingForOperand = false;
}

void calculator::changeSignClicked()
{
    QString text = display->text();
    double value = text.toDouble();

    if (value > 0.0) {
        text.prepend(tr("-"));
    } else if (value < 0.0) {
        text.remove(0, 1);
    }
    display->setText(text);
}

void calculator::backspaceClicked()
{
    if (waitingForOperand)
        return;

    QString text = display->text();
    text.chop(1);
    if (text.isEmpty()) {
        text = "0";
        waitingForOperand = true;
    }
    display->setText(text);
}

void calculator::clear()
{

    display->setText("0");
    waitingForOperand = true;
}

void calculator::clearAll()
{
    sumSoFar = 0.0;
    factorSoFar = 0.0;
    pendingAdditiveOperator.clear();
    pendingMultiplicativeOperator.clear();
    display->setText("0");

    waitingForOperand = true;
}

void calculator::clearMemory()
{
    sumInMemory = 0.0;
}

void calculator::readMemory()
{
    display->setText(QString::number(sumInMemory));
    waitingForOperand = true;
}

void calculator::setMemory()
{
    equalClicked();
    sumInMemory = display->text().toDouble();
}

void calculator::addToMemory()
{
    equalClicked();
    sumInMemory += display->text().toDouble();
}
button *calculator::createButton(const QString &text, const char *member)
{
    button *button1 = new button(text);
    connect(button1, SIGNAL(clicked()), this, member);
    return button1;
}

void calculator::abortOperation()
{
    clearAll();
    display->setText(tr("####"));
}

bool calculator::calculate(double rightOperand, const QString &pendingOperator)
{
    if (pendingOperator == tr("+")) {
        sumSoFar += rightOperand;
    } else if (pendingOperator == tr("-")) {
        sumSoFar -= rightOperand;
    } else if (pendingOperator == tr("37")) {
        factorSoFar *= rightOperand;
    } else if (pendingOperator == tr("37")) {
        if (rightOperand == 0.0)
            return false;
        factorSoFar /= rightOperand;
    }
    return true;
}

当计算器重叠在主window 小部件上时,我得到了输出。 我可以为计算器单独购买 window 吗?

您必须将 calc = new calculator(this) 更改为 calc = new calculator()

来自 Qt 文档

QWidget::QWidget ( QWidget * parent = 0, Qt::WindowFlags f = 0 )

Constructs a widget which is a child of parent, with widget flags set to f.

If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.

构造计算器时不提供parent,会变成单独的顶层window