程序出错了,各位大侠帮忙
头文件1 #ifndef TIMEREAD_H
2 #define TIMEREAD_H
3 #include <QtGui>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8 //#include <sys/ioctl.h>
9 #include <sys/types.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 //#include <linux/fs.h>
13 #include <string.h>
14 #include <stdbool.h>
15 #include <QWidget>
16 namespace Ui {
17 class TimeRead;
18 }
19 class TimeRead : public QWidget
20 {
21 Q_OBJECT
22 public:
23 explicit TimeRead(QWidget *parent = 0);
24 ~TimeRead();
25 private:
26 Ui::TimeRead *ui;
27 public:
28 QSocketNotifier *notifier;
29 private slots:
30 void readtime();
31 };
32 #endif // TIMEREAD_H
源文件
1 #include "timeread.h" 2 #include "ui_timeread.h"
3 int fd;
4 unsigned long t[2];
5 //double time; 6 static int len;
7 static char buffer[30]; 8
9 TimeRead::TimeRead(QWidget *parent) :
10 QWidget(parent),
11 ui(new Ui::TimeRead)
12 {
13 fd = open("/dev/my_button", O_RDONLY);
14 if (fd < 0) {
15 perror("Open /dev/my_button failed.\n");
16 }
17 ui->setupUi(this);
18 notifier = new QSocketNotifier(fd,QSocketNotifier::Read,this);
19 connect(notifier,SIGNAL(activated(int)),this,SLOT(readtime()));
20 } 21
22 TimeRead::~TimeRead() 23 {
24 delete ui; 25 }
26 void TimeRead::readtime()
27 {
28 len=read(fd,buffer,sizeof buffer-1);
29 if(len>0)
30 {
31 time=(t[0]/100.0)+(t[1]/100.0/50625.0);
32 buffer[len]='\n';
33 sscanf(buffer,"time=%.2lf s\n\n",&time);
34 }
35 else
36 {
37 perror("failed!");
38 }
39 QString tempStr;
40 ui->lineEdit->setText(tempStr.setNum(time));
41 }
这是我编的一段代码,在windows下的Qtcreator中可以编译成功,但是在linux编译时出现错误,错误如下
timeread.cpp: In member function 'void TimeRead::readtime()':
timeread.cpp:34: error: assignment of function 'time_t time(time_t*)'
timeread.cpp:34: error: cannot convert 'double' to 'time_t(time_t*)throw ()' in assignment
timeread.cpp:36: warning: unknown conversion type character '.' in format
timeread.cpp:36: warning: too many arguments for format
timeread.cpp:43: error: call of overloaded 'setNum(time_t (&)(time_t*)throw ())' is ambiguous
各位大侠帮忙解决一下 --------------------编程问答--------------------
楼主应该注意函数的变量类型问题,34行错误不属于qt,是scanf的用法问题,注意里面的参数类型, 可以加上自己的强制类型转换。
QString & QString::setNum ( int n, int base = 10 )
这是第43行的函数原型。 tempStr.setNum(time)中的time 应该是int 类型的。
楼主贴程序的时候最好把全局变量的声明贴出来, 要不不知道 全局变量的类型, 没法对你的程序作出准确理解判断。 --------------------编程问答--------------------
楼上正解 --------------------编程问答--------------------
QString & setNum ( int n, int base = 10 )
QString & setNum ( uint n, int base = 10 )
QString & setNum ( long n, int base = 10 )
QString & setNum ( ulong n, int base = 10 )
QString & setNum ( qlonglong n, int base = 10 )
QString & setNum ( qulonglong n, int base = 10 )
QString & setNum ( short n, int base = 10 )
QString & setNum ( ushort n, int base = 10 )
QString & setNum ( double n, char format = 'g', int precision = 6 )
QString & setNum ( float n, char format = 'g', int precision = 6 )
补充:移动开发 , Qt