Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Question About Calculator Example



  • I have a question about the example mentioned here.

    I'm trying to code the example app in Visual Studio 2019. My compiler is giving this error on the lines of code where a Button class object is passed to the QGridLayout::addWidget() method:

    no instance of overloaded function "QGridLayout::addWidget" matches the argument list
    

    This is my current Calculator.cpp file:

    #include "Calculator.h"
    #include <QtWidgets/qwidget.h>
    #include <QtWidgets/qlineedit.h>
    #include <QtWidgets/qgridlayout.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 (std::size_t i{}; i < NumDigitButtons; ++i) {
    		digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
    	}
    
    	Button* pointButton{ createButton(tr("."), SLOT(pointClicked())) };
    	Button* changeSignButton{ createButton(tr("\302\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 (std::size_t i{}; i < NumDigitButtons; ++i) {
    		int row = ((9 - i) / 3) + 2;
    		int column = ((i - 1) % 3) + 1;
    		mainLayout->addWidget(digitButtons[i], row, column);
    	}
    
    	mainLayout->addWidget(digitButtons[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"));
    }
    

    This and the Calculator.h file are all I have right now.

    By the way, I also want to know how I can use the Qt Tools for VS2019 to build a Qt project. I installed the extension from the Visual Studio marketplace but I don't know how to use it. And another thing: I want to use smart pointers here instead of raw pointers, but first I'd like to ask: would it be best to just use std::unique_ptr<>s, or are there places where a std::shared_ptr<> would be better?

    I also want to know how I can change the color of the buttons and and stuff. I want to make it look and feel different enough from the example that if I add it to my portfolio, people can't call me out on just using a tutorial and not putting in any effort myself. I'd like some suggestions on that. Please and thank you.

    Anyway, yeah, thanks for any help. [Note: my end goal is to create an Electron app out of this to put on my portfolio. A WebAssembly+Electron app. I'll open a new thread in the Qt for WebAssembly section of the forum after I'm done here. Just a heads-up.]


  • Lifetime Qt Champion

    Is Button derived from QWidget?



  • It's derived from QToolButton. Deriving from it gives the error conversion to inaccessible base class "QToolButton" is not allowed., though. The other error is gone now. I #included QtWidgets/qtoolbutton.h, by the way.


  • Lifetime Qt Champion

    You should derive public from QToolButton, not private.



  • That fixed it. I don't know why I did it, but I just put class Button : QToolButton. But yeah, thanks for now.

    I still need to know how to use that Qt Tools for VS extension, but I'll get to that after the code is all written.



  • What about my question on switching from raw pointers to smart pointers in the app's code? Should I just use std::unique_ptr throughout the whole thing? Once again for reference, it's this example. My reasoning is that I want to follow the RAII principle. Raw pointers don't follow it.


  • Lifetime Qt Champion

    @DragonOsman For QObject derived classes you should not use smart pointers as you will get double deletes. Simply set parent in such objects and the parent will delete its children. See https://doc.qt.io/archives/qt-4.8/objecttrees.html



  • So you mean I just need to pass a smart pointer as the argument to the parent parameter in the constructor? And are all of the classes I'm using derived from QObject?

    Also, how do I change the colors of widgets? Along with that, I also think I'll add some more buttons if it won't ruin the grid layout. I want to add parentheses (two buttons here), a function to raise to nth power, and a function to take the nth root. I know the latter two are unary functions, but what category would the parenthesis buttons fall under?


  • Lifetime Qt Champion

    @DragonOsman said in Question About Calculator Example:

    pass a smart pointer as the argument to the parent

    No! RAW pointer.

    "how do I change the colors of widgets?" - see https://doc.qt.io/qt-5/stylesheet-syntax.html

    "but what category would the parenthesis buttons fall under?" - I don't understand this question. A button is a button, doesn't matter what action it triggers.



  • @DragonOsman
    Some classify parentheses in a category of their own, e.g. https://chortle.ccsu.edu/java5/Notes/chap09B/ch09B_2.html

    An expression is a combination of literals, operators, variable names, and parentheses used to calculate a value.

    https://softwareengineering.stackexchange.com/questions/208350/what-do-you-call-parentheses-operators is worth a read through. there the accepted answer states "grouping operators" for your case:

    Parentheses ("(" and ")") are operators when used in an expression like a*(b+c), in which case they're often referred to as grouping operators.

    The other answer describes "parentheses operators" as punctuators.

    So take your pick :)



  • Thanks for the reply.

    I tried to build the project in VS2019 using the Qt VS2019 Tools, but the build failed due to MSBuild errors. I hope someone on here can help me, though I may have to consult the MSDN forums too since it's about MSBuild. Gist link.

    I tried to build it with VS2019 normally before this as well and it failed due to linker errors. If you want me to, I could try that again. But please tell me what .lib files to link against please. Thanks.



  • I was able to build the example in Qt Creator. I'm going to try extending it from here. I want to try making it into a scientific calculator, but I need some help.

    You know how there's this call to tr() in the example code? tr("x\302\262"). I want to know how to do this to get the "x to the power of (any given) n" symbol.

    Also, when implementing the functions for parentheses, do I just write code to append parentheses?



  • @DragonOsman said in Question About Calculator Example:

    "x\302\262" x²

    You can use the caret (^) as exponentiation operator.


Log in to reply