2013-02-15 5 views
0

저는 Qt를 처음 사용하고 있으며, 눌려진 신호와 플라스 모이드를 쓰는 기능이있는 누름 단추를 연결하려고합니다. 외부 함수를 Plasma :: PushButton 유형 객체로 공용으로 사용하려고 시도 했으므로 클릭 한 신호와 연결하는 데 사용할 수 있습니까? gtk를 사용하면 클릭 횟수가 간단 해졌습니다.하지만 슬롯을 사용하지 않아도되는 Qt의 간단한 방법은 없습니다.플라즈 모이드에 위젯 신호를 연결하십시오.

#include "plasma-tutorial1.h" 
#include<string.h> 
#include <stdlib.h> 
#include <QGraphicsLinearLayout> 
#include <QPainter> 
#include <QFontMetrics> 
#include <QSizeF> 
#include <QTextEdit> 
#include <plasma/svg.h> 
#include <plasma/theme.h> 
#include <plasma/widgets/lineedit.h> 
#include <plasma/widgets/pushbutton.h> 
#include <plasma/widgets/textedit.h> 
//PlasmaTutorial1 mainf; 

themainfunction(int choice,Plasma::LineEdit *m_lineEdit, 
Plasma::TextEdit *m_displaytext) 
{ 
char buffer[100]; 
const char *inp=(const char *)malloc(sizeof(char)*100); 
QString input; 
input=m_lineEdit->text(); 
QByteArray byteArray = input.toUtf8(); 
inp = byteArray.constData(); 
char *expression; 
expression=(char *)malloc(sizeof(char)*100); 
strcpy(expression,"sudo apt-"); 

switch(choice) 
{ 
case 1:{ 
     strcat(expression,"get install "); 
     strcat(expression,inp); 
     break; 
     }; 
case 2:{ 
     strcat(expression,"get remove "); 
     strcat(expression,inp); 
     break; 
     }; 
case 3:{ 
     strcat(expression,"cache search "); 
     strcat(expression,inp); 
     break; 
     }; 
}; 
/* 
FILE* in; 
FILE* ptr=fopen("new.txt","w"); 
FILE *popen(); 
if(!(in = popen("cal","r"))) 
    {return;} 

    while(fgets(buffer, 100, in) != NULL) { 
*/ 
m_displaytext->append("yeah!"); 
// fprintf(ptr,"%s",buffer); 

      //} 
//pclose(in); 
//fclose(ptr); 
} 
PlasmaTutorial1::PlasmaTutorial1(QObject *parent, const QVariantList &args) 
    : Plasma::Applet(parent, args), 
    m_svg(this), 
    m_icon("document") 
{ 
    m_svg.setImagePath("widgets/background"); 
    // this will get us the standard applet background, for free! 
    setBackgroundHints(DefaultBackground); 
    resize(200, 200); 
} 


PlasmaTutorial1::~PlasmaTutorial1() 
{ 
    if (hasFailedToLaunch()) { 
     // Do some cleanup here 
    } else { 
     // Save settings 
    } 
} 

void PlasmaTutorial1::init() 
{ 

    /* // A small demonstration of the setFailedToLaunch function 
    if (m_icon.isNull()) { 
     setFailedToLaunch(true, "No world to say hello"); 
    }*/ 
Counter rt; 
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this); 
    layout->setOrientation(Qt::Vertical); //so widgets will be stacked up/down 

    m_lineEdit = new Plasma::LineEdit(this); 
    m_lineEdit->setText("Enter the package name here"); 

    m_displaytext = new Plasma::TextEdit(this); 
    m_displaytext->setText("Terminal"); 

    m_installButton = new Plasma::PushButton(this); 
    m_installButton->setText("Install"); 
    connect(m_installButton, SIGNAL(clicked()),&rt,SLOT(themainfunction(1,m_lineEdit,m_displaytext))); 

    m_removeButton = new Plasma::PushButton(this); 
    m_removeButton->setText("Remove"); 
    // m_removeButton->clicked() 

    connect(m_removeButton, SIGNAL(clicked()),&rt,SLOT(themainfunction(2,m_lineEdit,m_displaytext))); 

    m_searchButton = new Plasma::PushButton(this); 
    m_searchButton->setText("Search"); 
    connect(m_searchButton, SIGNAL(clicked()),&rt,SLOT(themainfunction(3,m_lineEdit,m_displaytext))); 


    layout->addItem(m_lineEdit); 
    layout->addItem(m_installButton); 
    layout->addItem(m_removeButton); 
    layout->addItem(m_searchButton); 
    layout->addItem(m_displaytext); 
    m_displaytext->append("yo baby!");  
} 

/* 
void PlasmaTutorial1::paintInterface(QPainter *p, 
     const QStyleOptionGraphicsItem *option, const QRect &contentsRect) 
{ 
    p->setRenderHint(QPainter::SmoothPixmapTransform); 
    p->setRenderHint(QPainter::Antialiasing); 

    // Now we draw the applet, starting with our svg 
    m_svg.resize((int)contentsRect.width(), (int)contentsRect.height()); 
    m_svg.paint(p, (int)contentsRect.left(), (int)contentsRect.top()); 

    // We place the icon and text 
    p->drawPixmap(7, 0, m_icon.pixmap((int)contentsRect.width(),(int)contentsRect.width()-14)); 
    p->save(); 
    p->setPen(Qt::white); 
    p->drawText(contentsRect, 
       Qt::AlignBottom | Qt::AlignHCenter, 
       "Hello Plasmoid!"); 
    p->restore(); 
} 
*/ 
// This is the command that links your applet to the .desktop file 
K_EXPORT_PLASMA_APPLET(tutorial1, PlasmaTutorial1) 

#include "plasma-tutorial1.moc" 

나는 설치 버튼을 클릭 할 때 themainfunction()을 연결하려고합니다. 나는 그것을 어떻게합니까? 플라스마로 수업을 시작하려면 어떻게해야합니까?

+0

일부 코드를 제공하고 문제에 대해 좀 더 자세히 설명해 주시겠습니까? 또한 약간의 설명서를 읽는 것이 도움이 될 수 있습니다. http://doc.qt.digia.com/4.7-snapshot/signalsandslots.html – kfunk

+0

Q 문자열을 사용하여 C 스타일의 문자열 연산을 수행하는 것을 중지하고, 시간을 낭비하고 잠재적으로 코드에 버그를 도입하게됩니다. 슬롯에 대해서는 슬롯처럼 작동하지 않습니다. kfunk가 지적한대로 문서를 읽으십시오. 그런 매개 변수는 전달할 수 없으며 슬롯은 자유 함수가 될 수 없습니다. – Mat

+0

플라스마에서 푸시 버튼 클래스를 어떻게 유도 할 수 있습니까? 나는 그것을 시도했지만 많은 오류에 직면했다. 슬롯 기능을 사용할 수 있도록이 작업을 수행 할 수있는 방법을 제공 할 수 있습니까? – mjnovice

답변

0

나는 어디에서 빠져 나갔어. 슬롯 함수에서 객체를 인수로 전달할 수 없습니다. 이제는 부드럽습니다. 모든 답장에 대해 고맙습니다.

+0

답을 정답으로 표시하십시오. – troyane