Kod: Markera allt
class cl1
{
int a,b;
int *ptr, *ptr1;
public:
cl1();
void foo();
void bar();
};
Kod: Markera allt
#include "function.h"
#include <unistd.h>
#include <iostream>
cl1::cl1()
{
using namespace std;
cout << "cl1:b #1=" << cl1::b << endl;
cout << "cl1:ptr1 #1=" << cl1::ptr1 << endl;
b=4;
ptr1=&b;
cout << "cl1:b #2=" << cl1::b << endl;
cout << "cl1:ptr1 #2=" << cl1::ptr1 << endl;
}
void cl1::foo()
{
using namespace std;
a=1;
cout << "foo:a="<< a << endl;;
cout << "foo:ptr #1=" << cl1::ptr << endl;
ptr=&a;
cout << "foo:ptr #2=" << cl1::ptr << endl;
}
using namespace std;
void cl1::bar()
{
cout << "bar:a="<< a << endl;
cout << "bar:ptr=" << ptr << endl;
cout << "bar:b=" << cl1::b << endl;
cout << "bar:ptr1=" << cl1::ptr1 << endl;
}
Kod: Markera allt
#include "function.h"
int main(int argc, char *argv[])
{
cl1 myCl1;
myCl1.foo();
myCl1.bar();
}
- cl1:b #1=0
cl1:ptr1 #1=0x7ffce4472cf0
cl1:b #2=4
cl1:ptr1 #2=0x7ffce4472bf4
foo:a=1
foo:ptr #1=0x400a00
foo:ptr #2=0x7ffce4472bf0
bar:a=1
bar:ptr=0x7ffce4472bf0
bar:b=4
bar:ptr1=0x7ffce4472bf4
Men med det jag vill köra är ett annat lite mer komplext test som inte fungerar som jag vill. Det jag vill göra är att anropa en funktion, setTest(), som tar en global pekare, matrix12, och setTest() anropar i sin tur setMatrix(MyWindow::matrix12, MyWindow::img1p); Anledningen att jag inte anropar setMatrix direkt med parametar är att jag har en knapp, emit, som skickar Qt-signal till slot, setTest(). Om man har knappar i Qt kan man inte skicka parametrar om jag fattat rätt.
Jag har utgått från:https://www.linux-apps.com/p/1132152/
Som jag modiferat lite då jag vill få flytande text som är mitt slutmål. Nedan koden som jag har problem med.
Kod: Markera allt
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QtGui>
#include <qledmatrix.h>
#define Debug
class MyWindow : public QWidget
{
Q_OBJECT
public:
MyWindow();
void setMatrix1();
QLedMatrix matrix14;
void setMatrix(QLedMatrix *matrix, QImage *img);
QLedMatrix *matrix12;
QImage *img1p;
public slots:
// void WClear();
void setTest();
private:
QVBoxLayout *mainLayout;
};
#endif // MYWINDOW_H
Kod: Markera allt
#include "mywindow.h"
#include <iostream>
MyWindow::MyWindow()
{
setWindowTitle("Signals and Slots Example3");
using namespace std;
QRadioButton *radioButton = new QRadioButton("Clear");
QRadioButton *radioButton1 = new QRadioButton("emit", this);
mainLayout = new QVBoxLayout;
const int img1_col=60,img1_row=16;
QLedMatrix *matrix12 = new QLedMatrix;
matrix12->setColumnCount(img1_col);
matrix12->setRowCount(img1_row);
cout << "matrix12=";
cout << matrix12 << endl;
mainLayout->addWidget(matrix12);
mainLayout->addWidget(radioButton);
mainLayout->addWidget(radioButton1);
setLayout(mainLayout);
connect(radioButton, SIGNAL(clicked()), matrix12, SLOT(EClear()));
connect(radioButton1, SIGNAL(clicked()), this, SLOT(setTest()));
QImage img1(":/erik8.png");
QImage *img1p;
img1p=&img1;
cout << "matrix12="<< matrix12 << endl;
setMatrix(matrix12, img1p);
cout << "After call, matrix12="<< matrix12 << endl;
cout << "matrix12="<< matrix12 << endl;
cout << "img1p=" << img1p << endl;
}
void MyWindow::setTest()
{
using namespace std;
cout << endl;
cout << "setTest nr1" << endl;
cout << "matrix12="<< MyWindow::matrix12 << endl;
cout << "img1p=" << MyWindow::img1p << std::endl;
cout << "matrix14="<< &MyWindow::matrix14 << std::endl;
cout << "setTEST nr2" << std::endl;
setMatrix(MyWindow::matrix12, MyWindow::img1p);
}
void MyWindow::setMatrix(QLedMatrix *matrix, QImage *img) {
#ifdef Debug
using namespace std;
cout << endl;
cout << "setMatrix nr1 pil->" << endl;
cout << "matrix="<< matrix << endl;
cout << "matrix12="<< matrix12 << endl;
cout << "setMatrix nr2 pil->" << endl;
cout << endl;
#endif
for(int row=0; row < matrix->rowCount(); ++row)
{
for(int col=0; col < matrix->columnCount(); ++col) {
if(img->pixel(col+3, row) == QLedMatrix::White) {
matrix->setColorAt(row, col, QLedMatrix::Yellow); }
else {
matrix->setColorAt(row, col, QLedMatrix::Blue); }
}
}
}
- $ ./qledmatrix_demo
matrix12=0xcc85a0
matrix12=0xcc85a0
setMatrix nr1 pil->
matrix=0xcc85a0
matrix12=0
setMatrix nr2 pil->
After call, matrix12=0xcc85a0
matrix12=0xcc85a0
img1p=0x7fff2dafdba0
- setTest nr1
matrix12=0
img1p=0x4040f0
matrix14=1
setTEST nr2
setMatrix nr1 pil->
matrix=0
matrix12=0
setMatrix nr2 pil->
Segmentation fault (core dumped)
Vaför blir det "fel"? Vad är det för skillnad i de olika exemplen?
Vad skall jag åtgärda så att det blir som jag vill?
bildexempel finns här:
http://elektronikforumet.com/forum/view ... 43&t=89504