Seite 1 von 1

Button erscheint nicht

Verfasst: 2. Juni 2006 16:35
von Karldin Shinowa

Code: Alles auswählen

//Button.h
#ifndef Button_h
#define Button_h


#include<QWidget>

class QPushButton;

class Button:public QWidget
{
   private:
      QPushButton *button;
      const short value;//ein Button hat eine Zahl drauf(is für nen rechner)

   public:
      Button(int value,int x,int y,int w,int h, QWidget*parent=0);
      short getValue() const;
};

#endif

Code: Alles auswählen

//Button.cpp
#include"Button.h"
#include<QPushButton>
#include<QVBoxLayout>

Button::Button(int value,int x,int y,int w,int h, QWidget*parent):
   QWidget(parent),value(value)
{
   char*name=new char;
   name[0]=static_cast<char>(value+static_cast<int>('0'));//improvisiert 
  
   button=new QPushButton(name,0);
  
   QVBoxLayout*layout=new QVBoxLayout;
   layout->addWidget(button);
   setLayout(layout);

   this->setGeometry(x,y,w,h);
}
      


short Button::getValue() const
{
   return this->value;
}

Code: Alles auswählen

//main.cpp
#include<QApplication>
#include<QWidget>
#include"Button.h"

int main(int argc,char*argv[])
{
   QApplication *app=new QApplication(argc,argv);
   
   QWidget* MyWindow=new QWidget;
   MyWindow->setFixedSize(100,100);
   
   Button myButton(1,10,10,20,20,MyWindow);

   MyWindow->show();


   return app->exec();
}
Jetzt erscheinen statt dem Button 3 komische zeichen...
Ich schätze der Fehler liegt im Konstruktor oder es ist ein denkfehler

Verfasst: 2. Juni 2006 18:49
von Karldin Shinowa
so ein bissi rum gebastelt. jetzt zeigt er das zeichen was ich will an allerdings keinen button. nur den grauen hintergrund und ne 1 drauf..
hier das geänderte:

Code: Alles auswählen

//Button.cpp


#include"Button.h"
#include<QPushButton>
#include<QVBoxLayout>
#include<string>
//////NEU///////
#include <sstream>
std::string number2string(int nr){
    std::stringstream ss;
    ss << nr;
    return ss.str();
}
////////////////////
Button::Button(int value,int x,int y,int w,int h, QWidget*parent):
   QWidget(parent),value(value)
{
   //////NEU//////
   std::string name=number2string(value);
  
   button=new QPushButton(name.c_str(),0);
   ////////////////
  
   QVBoxLayout*layout=new QVBoxLayout;
   layout->addWidget(button);
   setLayout(layout);

   this->setGeometry(x,y,w,h);
}
      

short Button::getValue() const
{
   return this->value;
}

Verfasst: 2. Juni 2006 22:57
von jaegermeister
Hi
Versuch ma:

Code: Alles auswählen

Button::Button(int value,int x,int y,int w,int h, QWidget*parent):
   QWidget(parent),value(value)
{
   //////NEU//////
   std::string name=number2string(value);
 
//   button=new QPushButton(name.c_str(),0); //ersetzt durch die nächste zeile
   button=new QPushButton(name.c_str(),parent);//<- hier nicht 0 sondern parent
//edit: geb als parent this an, wäre logischer glaub ich 
   ////////////////
 
   QVBoxLayout*layout=new QVBoxLayout;
   layout->addWidget(button);
   setLayout(layout);

   this->setGeometry(x,y,w,h);
}
 
Ich denke aber, dass es das nicht sein kann weil du ja den PushButton aufs Layout setzt, aber probieren geht über studieren. ;)

BTW: Wieso leitest du deinen Button nicht von QAbstractButton oder QPushButton ab?

mfg jaegermeister