读书人

QT中难以解决的有关问题

发布时间: 2012-12-29 10:28:09 作者: rapoo

QT中难以解决的问题


/********************dialog.h****************************/
#include <QApplication>
#include<QWidget>
#include<QtGui>
#include"printtext.h"

class Dialogs : public QWidget
{
Q_OBJECT
public:
Dialogs();
private:
QFrame *frame;
QPushButton *button0;
QPushButton *button1;
QPushButton *button2;
QTextEdit *textEdit;
private slots:
void slotbutton0();
void slotbutton1();
void slotbutton2();
// void slotbutton3();

};

/********************dialog.cpp****************************/
#include <QApplication>
#include<stdio.h>
#include<unistd.h>
#include <QtGui>
#include"printtext.h"
#include"dialog.h"

QFile file("printer.txt");
int pid;

Dialogs:ialogs( )

{
frame=new QFrame(this);
button0=new QPushButton(this);
button1=new QPushButton(this);
button2=new QPushButton(this);
frame->setObjectName("myframe");
frame->resize(320,240);
frame->setStyleSheet("QFrame#myframe{border-image:url(images/background.png)}" );

button0->setGeometry(260,30,50,50);
button1->setGeometry(260,90,50,50);
button2->setGeometry(260,150,50,50);

QIcon icon;
QPixmap pixmap0("images/Start_amended.png");
icon.addPixmap(pixmap0);
button0->setIcon(icon);
button0->setIconSize(QSize(50,50));
button0->setFixedSize(pixmap0.size());
button0->setMask(pixmap0.mask());

QPixmap pixmap1("images/Stop_amended.png");
icon.addPixmap(pixmap1);
button1->setIcon(icon);
button1->setIconSize(QSize(50,50));
button1->setFixedSize(pixmap1.size());
button1->setMask(pixmap1.mask());


QPixmap pixmap2("images/set.png");
icon.addPixmap(pixmap2);
button2->setIcon(icon);
button2->setIconSize(QSize(50,50));
button2->setFixedSize(pixmap2.size());
button2->setMask(pixmap2.mask());

connect(button0,SIGNAL(clicked()),this,SLOT(slotbutton0()));
connect(button1,SIGNAL(clicked()),this,SLOT(slotbutton1()));
connect(button2,SIGNAL(clicked()),this,SLOT(slotbutton2()));
// connect(button3,SIGNAL(clicked()),this,SLOT(slotbutton3()));

}

void Dialogs::slotbutton0()
{
execlp("sh","sh","hello.sh",NULL);
pid=getpid();
}

void Dialogs::slotbutton1()
{


// char a[10];
// sprintf(a,"%s",pid);
// execlp("kill","kill",a,NULL);
}

void Dialogs::slotbutton2()
{
// QWidget *w1=new QWidget;
textEdit=new QTextEdit(this);
// w1->addwidget(textEdit,0,0);
if(file.open(QIODevice::ReadOnly|QIODevice::Text))
{
QTextStream textStream(&file);
while(!textStream.atEnd())
{
textEdit->append(textStream.readLine());
}
file.close();
textEdit->show();

}

}
//void Dialogs::slotbutton3()
//{
// QTextStream out(&file);
// out<<textEdit->toPlainText();
// file.close();
//}
/********************main.cpp****************************/
#include <QApplication>
#include<stdio.h>
#include<unistd.h>
#include <QtGui>
#include"dialog.h"

int main( int argc, char **argv )
{


QApplication a( argc, argv );
Dialogs *dialogs = new Dialogs();
// Dialogs *dialogs;
dialogs->show();
return a.exec();
}


目前有三个问题:
1、在这里面我用到了背景图片,但不知道为什么背景图片不能显示(由一个独main函数改成的)
2、这里面我为了执行一个脚本程序用到了exec族函数,实际上就覆盖了这个程序,QT退出,实际上我要它只是一个桌面不能退出,有没有别的函数能代替exec函数后台执行脚本程序
3、按下第3个按钮会读出一个文件内容,现在想实现弹出一个框架,上面有保存和退出两个键,试过多次后总是不能正常编译成功,求高人指点!
[解决办法]
你的图片是在qrc中还是用的相对路径?如果在qrc中前面要加个“:”
[解决办法]
引用:
用的是相对路径!


问题不是出在路劲,而是用法不对:

楼主直接这样就行了,setico的参数可以是直接传入QPixmap的。
不信楼主试试看 ,反正试一下又不要钱:
QPixmap pixmap0("images/Start_amended.png");
button2->setIcon(pixmap0);

读书人网 >QT开发

热点推荐