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

Posted

技术标签:

【中文标题】在 QPushButton 上调用另一个小部件应用程序【英文标题】:Call another widget application on QPushButton 【发布时间】:2017-03-06 04:51:13 【问题描述】:

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

主窗口.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();


计算器.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("\30\261"),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("\303\267"), SLOT(multiplicativeOperatorClicked()));
button *timesButton = createButton(tr("\303\227"), 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("x\302\262"), 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("x\302\262")) 
        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("\303\227")) 
        factorSoFar *= rightOperand;
     else if (pendingOperator == tr("\303\267")) 
        if (rightOperand == 0.0)
            return false;
        factorSoFar /= rightOperand;
    
    return true;

当计算器在主窗口小部件上重叠时,我得到输出。 我可以为计算器获得单独的窗口吗?

【问题讨论】:

可以显示类计算器的代码。 我已经编辑了问题并添加了calculator.cpp @eyllanesc 【参考方案1】:

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

【讨论】:

【参考方案2】:

来自 Qt 文档

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

构造一个作为父级子级的小部件,并设置小部件标志 到 f。

如果 parent 为 0,则新窗口小部件成为窗口。如果父母是另一个 小部件,此小部件成为父窗口内的子窗口。新的 小部件在其父级被删除时被删除。

构造计算器时不提供父级,会变成单独的顶层窗口

【讨论】:

以上是关于在 QPushButton 上调用另一个小部件应用程序的主要内容,如果未能解决你的问题,请参考以下文章

QPushButton在顶部另一个小部件上对齐

在多个小部件上显示相同的 QPushButton

QPushButton 更改同一布局中其他小部件的边距

样式化的*** QPushButton 小部件无法正确呈现

调用父窗口小部件函数

PyQt4:使用 QPushButton 小部件从 QList 小部件中删除项目