Seite 1 von 1

Signal Slots Problem

Verfasst: 13. Mai 2006 21:44
von Karldin Shinowa
Also mag ein programm dass 2 buttons:- & + hat und man damit nen lcd steuern kann

Code: Alles auswählen

//LCD.h die Klasse des lcds

#include<QWidget>

class QLCDNumber;

class LCD:public QWidget
{
   Q_OBJECT
  
   public:
      LCD(QWidget*parent=0);
  
   private:
      int value;
      QLCDNumber*lcd;

   public slots:
      void incrementValue();
      void decrementValue();
};

Code: Alles auswählen

//LCD.cpp die definitionen
#include"LCD.h"
#include<QLCDNumber>
#include <QVBoxLayout>

LCD::LCD(QWidget*parent)
   :QWidget(parent)
{
   lcd=new QLCDNumber(2);
   lcd->setSegmentStyle(QLCDNumber::Filled);

   connect(this,SIGNAL(incrementValue()),lcd,SLOT(display(value)));
   connect(this,SIGNAL(decrementValue()),lcd,SLOT(display(value)));
  
   QVBoxLayout*layout=new QVBoxLayout;
   layout->addWidget(lcd);
   setLayout(layout);
}

void LCD::incrementValue()
{
   ++value;
   if(value>99) value=99;
}

void LCD::decrementValue()
{
   --value;
   if(value<0) value=0;
}

Code: Alles auswählen

//main.cpp
#include<QApplication>
#include<QPushButton>
#include<QFont>
#include<QGridLayout>
#include<QVBoxLayout>
#include"LCD.h"

class MyWidget:public QWidget
{
   public:
      MyWidget(QWidget*parent=0);
  
   private:
      QPushButton*plus;
      QPushButton*minus;
      LCD*lcd;
      
};

MyWidget::MyWidget(QWidget*parent)
{
   plus=new QPushButton("+");
   minus=new QPushButton("-");

   lcd=new LCD;

   connect(plus,SIGNAL(clicked()),
           lcd, SLOT(incrementValue()));
  
   connect(minus,SIGNAL(clicked()),
           lcd,  SLOT(decrementValue()));

   QGridLayout*grid=new QGridLayout;
   grid->addWidget(minus,0,0);
   grid->addWidget(plus,0,1);

   QVBoxLayout*layout=new QVBoxLayout;

   layout->addLayout(grid);
   layout->addWidget(lcd);
   setLayout(layout);
}


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    MyWidget window;
    window.show();

    return app.exec();
}

Er zeigt jetzt alles an aber wenn ich auf die buttons gehe verändert sich nix...[/code]

Verfasst: 13. Mai 2006 22:12
von upsala
Hmm, du programmierst unter Windows? Woher ich das weis? Du schaust dir schon wieder die Fehlermeldung auf der Konsole nicht an. Dort steht steht 100%ig das die Signale incrementValue und decrementValue nicht existieren.

Mach einfach in beiden Slots ein lcd->display(value)

Verfasst: 13. Mai 2006 22:31
von Karldin Shinowa
ne er sagt : [Linker error] undefined reference to `vtable for LCD'

Verfasst: 13. Mai 2006 22:57
von Schwarzherz
Die zwei connects in LCD.cpp kannst du weglassen, die hast du ja schon in der main.cpp hergestellt.

Das display() fehlt auch.

Weiters musst du value irgendwo initialisieren.

ZB so:

Code: Alles auswählen

//LCD.cpp die definitionen
#include"LCD.h"
#include<QLCDNumber>
#include <QVBoxLayout>

LCD::LCD(QWidget*parent)
   :QWidget(parent)
{
   lcd=new QLCDNumber(2);
   lcd->setSegmentStyle(QLCDNumber::Filled);
   value = 0;
 
   QVBoxLayout*layout=new QVBoxLayout;
   layout->addWidget(lcd);
   setLayout(layout);
}

void LCD::incrementValue()
{
   ++value;
   if(value>99) value=99;
   lcd->display(value);
}

void LCD::decrementValue()
{
   --value;
   if(value<0) value=0;
   lcd->display(value);
}

Verfasst: 14. Mai 2006 09:13
von ArneStocker
hi

Code: Alles auswählen

   connect(this,SIGNAL(incrementValue()),lcd,SLOT(display(value))); 
die Übergabe von value in SLOT(display(value)) ist unzulässig

http://qtforum.de/forum/viewtopic.php?t=1836

Gruß Arne

Verfasst: 14. Mai 2006 09:54
von Karldin Shinowa
so hier kommt die 2nd edition(wieder ein problem das das klicken auf die buttons nichts bewirkt...)

Code: Alles auswählen

//LCD.h
#include<QWidget>

class QPushButton;
class QLCDNumber;

class LCD:public QWidget
{
   Q_OBJECT

	private:
      QLCDNumber* lcd;
      QPushButton* plus;
      QPushButton* minus;
      int value;

   public slots:
      void increment();
      void decrement();

   public:
      LCD(QWidget*parent=0);
      int GetValue();
};

Code: Alles auswählen

//LCD.cpp
#include<QLCDNumber>
#include<QWidget>
#include<QPushButton>
#include<QFont>
#include<QGridLayout>
#include<QVBoxLayout>
#include"LCD.h"

LCD::LCD(QWidget*parent)
{
   lcd=new QLCDNumber(2);
   lcd->setSegmentStyle(QLCDNumber::Filled);

   value=50;
   lcd->display(value);

   plus=new QPushButton(tr("+"));
   minus=new QPushButton(tr("-"));

   QFont font("Arial",20);
   plus->setFont(font);
   minus->setFont(font);

   connect(plus,SIGNAL(clicked()),
           this, SLOT(incrementValue()));
  
   connect(minus,SIGNAL(clicked()),
           this,  SLOT(decrementValue()));

   QGridLayout* grid=new QGridLayout;
   grid->addWidget(minus,0,1);
   grid->addWidget(plus,0,2);

   QVBoxLayout* layout=new QVBoxLayout;
   layout->addLayout(grid);
   layout->addWidget(lcd);

   setLayout(layout);
}
void LCD::increment()
{
   ++value;
   if(value>99) value=99;
   lcd->display(value);
}

void LCD::decrement()
{
   --value;
   if(value<0) value=0;
   lcd->display(value);
}

int LCD::GetValue()
{
   return value;
}

Code: Alles auswählen

//main.cpp
#include"LCD.h"
#include<QApplication>

int main(int argc,char* argv[])
{
   QApplication app(argc,argv);
   LCD window;
  
   window.show();

   return app.exec();
}

Verfasst: 14. Mai 2006 13:40
von patrik08
es geht auch nur mit einem ui file....
source subversion ....

svn co http://ciz.ch/svnciz/xls2sql/tmp0/ nummer_increment

cd nummer_increment && qmake && make

Verfasst: 14. Mai 2006 17:20
von Karldin Shinowa
HÄH? :shock: :shock: Bin in QT noch Anfänger..

Verfasst: 14. Mai 2006 23:52
von ArneStocker

Code: Alles auswählen

public slots:
      void increment(); 

connect(plus,SIGNAL(clicked()),
           this, SLOT(incrementValue())); 
Dein Slot heisst auch increment() und nicht incrementValue(). Schreib doch mal

Code: Alles auswählen

 connect(plus,SIGNAL(clicked()),
           this, SLOT(increment())); 
Gruss Arne

Verfasst: 15. Mai 2006 09:56
von patrik08
das schreibt der ui selber... so:

Code: Alles auswählen

 QObject::connect(okButton, SIGNAL(clicked()), Nummer_Increment, SLOT(accept()));
    QObject::connect(horizontalSlider, SIGNAL(valueChanged(int)), lcdNumber, SLOT(display(int)));
    QObject::connect(plus, SIGNAL(clicked()), lcdNumber, SLOT(repaint()));
    QObject::connect(minus, SIGNAL(clicked()), lcdNumber, SLOT(repaint()));
    QObject::connect(plus, SIGNAL(clicked()), horizontalSlider, SLOT(addStep()));
    QObject::connect(minus, SIGNAL(clicked()), horizontalSlider, SLOT(subtractStep()));

subtractStep & addStep gehen zu horizontalSlider und nicht lcd ... obwohl dann horizontalSlider den lcd setzt....

Verfasst: 15. Mai 2006 20:36
von Karldin Shinowa
danke ArneStocker jetzt funtzt. hab den namen von version1 zu version2 geändert... copy paste :roll: :roll:

Verfasst: 15. Mai 2006 22:39
von Karldin Shinowa
Hier stand ein weiteres Problem das ich schon gelöst habe.
Frage: kann man einen slot nicht mit nem signal connecten?

connect(this,SIGNAL(myslot()),this,SLOT(mysignal()));