Qt 계산기좀 봐주세요~..

bigdaddy2001의 이미지

일딴 버튼을 다 사용자 지정클레스로 만들었어요

MyButton::MyButton(QWidget *parent, const QString& imageNorPass, const QString& imageSelPass)
    : QWidget(parent)
{
 
    view = new QGraphicsView(this);
    scene = new QGraphicsScene();
    imageSel = new QImage();
    imageNor = new QImage();
    view->setScene(scene);
 
 
    imageNor->load(imageNorPass);
    imageSel->load(imageSelPass);
 
    scene->addPixmap(QPixmap::fromImage(*imageNor));
 
}
 
 
void MyButton::mousePressEvent(QMouseEvent *)
{
    qDebug("********************** mousePressEvent");
 
    scene->addPixmap(QPixmap::fromImage(*imageSel));
 
    emit signalPrease(this);
}

이렇게 버튼 코드구용
mywidget::mywidget(const QString &filename, QWidget *parent): QWidget(parent)
{
 
   setWindowState(Qt::WindowFullScreen);
 
   memoryvalue = 0;
   waitvalue = true;
 
   textdisplay = new QLineEdit("", this);
   textdisplay->setText("0");
   textdisplay->setGeometry(5,38,710,83);
 
   ChangeButton = new MyButton(this, QString("Z:\\home\\kth\\test9\\test\\prj2\\image2\\calculator_titlebar_w.png"), QString("C:\\qt\\test\\prj2\\image2\\calculator_titlebar_btn_sel.png"));
   ChangeButton->setGeometry(715,39,79,82);
   connect(ChangeButton, SIGNAL(signalPrease(QWidget*,int,int)), this, SLOT(slotPrease(QWidget*,int,int)));
 
   Number0 = new MyButton(this, QString("Z:\\test9\\test\\prj2\\image2\\calculator_btn_0_nor.png"), QString("C:\\qt\\test\\prj2\\image2\\calculator_btn_0_sel.png"));
   Number0->setGeometry(402, 412, 178, 53);
   connect(Number0, SIGNAL(signalPrease(QWidget*,int,int)), this, SLOT(slotPrease(QWidget*,int,int)));
   Number0->show();
 
   Number1= new MyButton(this, QString("C:\\qt\\test\\prj2\\image2\\calculator_btn_1_nor.png"), QString("C:\\qt\\test\\prj2\\image2\\calculator_btn_1_sel.png"));
   Number1->setGeometry(402, 356, 87, 53);
   connect(Number1, SIGNAL(signalPrease(QWidget*,int,int)), this, SLOT(slotPrease(QWidget*,int,int)));

이렇게 버튼이 0~9까지 연산 + - / * 4가지 연산 = . 이렇게 총버튼이 16개 가량됩니다
그런데 버튼을 입력받으면 화면에 뿌려주는 코드가
void mywidget::slotPrease(QWidget* widget, int wparam, int lparam)
{
    if(widget == Number0)
    {
        value = 0;
        QString temp;
        temp.sprintf("%d", value);
        textdisplay->setText(temp);
 
    }
    else if(widget == Number1)
    {
        value = 1;
        QString temp;
        temp.sprintf("%d", value);
        textdisplay->setText(temp);
    }
    else if(widget == Number2)
    {
        value = 2;
        QString temp;
        temp.sprintf("%d", value);
        textdisplay->setText(temp);
    }else if(widget == Number3)
    .....

이런식으로햇더니 예를들면 7번버튼누르면 7 8번버튼 누르면8 이됩니다 789버튼 이렇게누르면 화면엔 9만
남게 되는것이지요..이것좀을 어떻게해야될까요 ㅠㅠㅠ 그리구 다른 연산도 도와 주십죠~ ㅎㅎ

lithium81의 이미지

기존 textDisplay에 있는 값을 다시 읽어서 10을 곱해준 후 새로 입력 받은 값을 더해주는 식으로 하면 될 것 같습니다만.
아래 코드는 그냥 대충대충 아는대로 쓴 거라 컴파일 안 될 수도 있는데, 컨셉이 뭔지 파악하는덴 문제 없을 거에요

...
else if (widget == Number7)
{
    QString currentValue = textdisplay->text();
 
    bool ok;
    int newValue = currentValue.toInt(&ok, 10) * 10 + 7;
 
    // 변수 ok가 false면 문제가 생긴 것이므로 체크하고 에러 처리.
 
    textdisplay->setText(QString::number(newValue, 10));
}
...

- 어제보다 나은 오늘, 오늘보다 나은 내일.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.